Click here to Skip to main content
13,087,566 members (92,591 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I got a issue with XML, and write some information on a XML-file.
I got several xsd-files describing my XML. I created one big .cs-file with
"xsd/c testfile1.xsd testFile2.xsd..."
etc. And everything went nice and looks good.
But if I take one created class i.e. testfile1.xsd, it looks like "<xs:complextype name="Header" xmlns:xs="#unknown">" and inside that one there is this some ordinary xs:element and stuff, but also this: "<xs:attribute name="version" default="" />". This is translated to:

"public Header() {
this.versionField = "";}"
in the generated class 'Header'. And it got as well this field: private string versionField;

. (There is of course a couple of other private fields as well, but those works good.). So I create instances of all classes, fill them with data and write it as an XML-file with this:

But when I look at the xml-file this <Header> is not have anything like version. I want i to look like: <Header version="">

Here is an piece of example-code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
namespace TestAppXML
    class Program
        static void Main(string[] args)
            RootInfo rootInfo = new RootInfo();
            rootInfo.RootText = "This is the Root!";
            Header header = new Header();
            header.TestHeader = "This is HeaderText!";
            rootInfo.Header = header;
            XmlSerializer XmlSerRoot = new XmlSerializer(typeof(RootInfo));
            StringWriterWithEncoding strWriter = new StringWriterWithEncoding(Encoding.GetEncoding("iso-8859-1"));
            XmlDocument documentInXML = new XmlDocument();
            XmlSerRoot.Serialize(strWriter, rootInfo);
            string XmlString;
            XmlString = strWriter.ToString();
    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public partial class RootInfo
        private Header headerField;
        private string rootTextField;
        public Header Header
            get { return this.headerField; }
            set { this.headerField = value; }
        [System.Xml.Serialization.XmlElementAttribute(DataType = "normalizedString")]
        public string RootText
            get { return this.rootTextField; }
            set { this.rootTextField = value; }
        [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
        [System.Xml.Serialization.XmlTypeAttribute(Namespace = "")]
        public partial class Header
            private string testHeaderField;
            private string versionField;
            public Header()
                this.versionField = "";
            /// <remarks/>
            [System.Xml.Serialization.XmlElementAttribute(DataType = "normalizedString")]
            public string TestHeader
                get { return this.testHeaderField; }
                set { this.testHeaderField = value; }
            public string version
                get { return this.versionField; }
                set { this.versionField = value; }
        class StringWriterWithEncoding : StringWriter
            private Encoding MyEncoding;
            public StringWriterWithEncoding(Encoding encoding)
                : base()
            {MyEncoding = encoding;}
            public override Encoding Encoding
                get{return MyEncoding;}

I even tried to do it in code like:
Header header = new Header(); 
header.version = header.version; 
with header.version = "";

But still it's no version-text in the tag. All other tags got their value. Just this <Header> loose this extra information.
Does someone got a tip? There is a lot of places I need this to work. Everything else is just working fine.

Posted 8-Apr-13 21:35pm
RedDK 19-Apr-13 14:22pm
Sure bet that the issue is by design with respect to the parser; I encounter the same behavior in TSQL while using the system command "sp_xml_preparedocument" under the guidance of OPENXML and other big methods. With some experimentation just like the last block above, but using REPLACE(etc,,) to get rid of the version information, I was able to back into the issue by retaining the version in the final output, even alter the version. But that's a post-parse appendix, so ... how to clear up this behavior in anything other than TSQL ... I guess I'm defending the reason behind parsing in general when I say that it would be ill-allotted time on behalf of a method to make an attempt to begin parsing an encoding that wasn't allowed because it's specified in that little preamble. The one that's so easily lopped off. So it bales.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Nevermind, I think I knov the issue. It's because this xsd.exe creates 'DefaultValueAttribute' for those fields. That prevent this field to be in the xml. Maybe some xsd-switch could have done that, I dunno...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170813.1 | Last Updated 9 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100