locked
XSD.exe class file fails with GroupOrder sequence RRS feed

  • Question

  • I have created a Class file  from a XSD using a XSD.exe. The instance of the XSD looks something like this..

    <EmployeeList>
    <CompanyID>123</CompanyID>
    <CompanyName>abc</CompanyName>
    <Employee>
            <EmployeeID>E123</Employee>
            <Grade>G123</Grade>
    </Employee>
    <Employee>
             <EmployeeID>E456</Employee>
             <Grade>G456</Grade>
    </Employee>
    </EmployeeList>

    I am serialising a XML file from the class file which I created above. The serialised output XML file look something like

    <EmployeeList>
    <Employee>
              <EmployeeID>E123</Employee>
               <Grade>G123</Grade>
    </Employee>
    <Employee>
               <EmployeeID>E456</Employee>
               <Grade>G456</Grade>
    </Employee>
    <CompanyID>123</CompanyID>
    <CompanyName>abc</CompanyName>
    </EmployeeList>

    The "<CompanyID>"  and "<CompanyName>" elements appears after the "<Employee>" record. But I want them to appear above the "<Employee>". Do I need to add anything to the class file to maintain  the sequence of the element and records?

     

    Thanks in advance.

    Monday, August 24, 2009 3:59 PM

Answers

  • We need to find out what is different in your case compared to the example I tried. So please provide the necessary details. Otherwise you might want to try and play with the Order property of the XmlElementAttribute to see whether using it helps you get the order you want.

    MVP XML My blog
    • Marked as answer by Yichun_Feng Monday, August 31, 2009 2:07 AM
    Monday, August 24, 2009 5:38 PM

All replies

  • Well <EmployeeID>E123</Employee> is not even well-formed. What have you done exactly with xsd.exe? Have you first inferred a schema from the XML instance document you have? And then generated .NET code from that schema? Have you checked how that schema looks?

    I have tried to reproduce the problem using .NET 3.5 SP 1 with Visual Studio 2008 SP 1 but when I use an XML document of the form

    <EmployeeList>
      <CompanyID>123</CompanyID>
      <CompanyName>abc</CompanyName>
      <Employee>
        <EmployeeID>E123</EmployeeID>
        <Grade>G123</Grade>
      </Employee>
      <Employee>
        <EmployeeID>E456</EmployeeID>
        <Grade>G456</Grade>
      </Employee>
    </EmployeeList>
    and have Visual Studio generate a schema then it looks like this:

    <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="EmployeeList">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="CompanyID" type="xs:unsignedByte" />
            <xs:element name="CompanyName" type="xs:string" />
            <xs:element maxOccurs="unbounded" name="Employee">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="EmployeeID" type="xs:string" />
                  <xs:element name="Grade" type="xs:string" />
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:schema>
    Then xsd.exe generates the following code:

    //------------------------------------------------------------------------------
    // <auto-generated>
    //     This code was generated by a tool.
    //     Runtime Version:2.0.50727.3082
    //
    //     Changes to this file may cause incorrect behavior and will be lost if
    //     the code is regenerated.
    // </auto-generated>
    //------------------------------------------------------------------------------
    
    using System.Xml.Serialization;
    
    // 
    // This source code was auto-generated by xsd, Version=2.0.50727.3038.
    // 
    
    
    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
    public partial class EmployeeList {
        
        private byte companyIDField;
        
        private string companyNameField;
        
        private EmployeeListEmployee[] employeeField;
        
        /// <remarks/>
        public byte CompanyID {
            get {
                return this.companyIDField;
            }
            set {
                this.companyIDField = value;
            }
        }
        
        /// <remarks/>
        public string CompanyName {
            get {
                return this.companyNameField;
            }
            set {
                this.companyNameField = value;
            }
        }
        
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("Employee")]
        public EmployeeListEmployee[] Employee {
            get {
                return this.employeeField;
            }
            set {
                this.employeeField = value;
            }
        }
    }
    
    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
    public partial class EmployeeListEmployee {
        
        private string employeeIDField;
        
        private string gradeField;
        
        /// <remarks/>
        public string EmployeeID {
            get {
                return this.employeeIDField;
            }
            set {
                this.employeeIDField = value;
            }
        }
        
        /// <remarks/>
        public string Grade {
            get {
                return this.gradeField;
            }
            set {
                this.gradeField = value;
            }
        }
    }
    
    and when I use

                EmployeeList eList = new EmployeeList();
                eList.CompanyID = 1;
                eList.CompanyName = "foo";
                eList.Employee = new EmployeeListEmployee[] {
                new EmployeeListEmployee() { EmployeeID = "e1", Grade = "g1" },
                new EmployeeListEmployee() { EmployeeID = "e2", Grade = "g2" }
                };
    
                XmlSerializer ser = new XmlSerializer(typeof(EmployeeList));
                ser.Serialize(Console.Out, eList);
    as an example to test the serialization the result is

    <EmployeeList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <CompanyID>1</CompanyID>
      <CompanyName>foo</CompanyName>
      <Employee>
        <EmployeeID>e1</EmployeeID>
        <Grade>g1</Grade>
      </Employee>
      <Employee>
        <EmployeeID>e2</EmployeeID>
        <Grade>g2</Grade>
      </Employee>
    </EmployeeList>
    which has the order you want.


    MVP XML My blog
    Monday, August 24, 2009 4:42 PM
  • Martin,

    Thanks for your reply. That was my typo. It should be 
    <EmployeeID>E123</EmployeeID>.

    The employee file which I have give is just a sample file.

    My intension was to generate a xml file from my C#-Helper class file. I wanted to create them as a typed representation, So created a XSD of the xml file and generated the class file of the same using the XSD.exe. Then I have initialized the class file as you did above. But in the output the sequence of the record was different

     

     

     

     

    Monday, August 24, 2009 5:19 PM
  • We need to find out what is different in your case compared to the example I tried. So please provide the necessary details. Otherwise you might want to try and play with the Order property of the XmlElementAttribute to see whether using it helps you get the order you want.

    MVP XML My blog
    • Marked as answer by Yichun_Feng Monday, August 31, 2009 2:07 AM
    Monday, August 24, 2009 5:38 PM