0

I'm following the solution from this question How to generate .NET 4.0 classes from xsd? to generate C# classes. But somehow it only generates the first element. Is there any way that I can generate all elements at same time?

Xsd doc looks like below:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <xs:element name="advice_file">
    </xs:element>
    <xs:element name="vendorMerchID" type="xs:string">
    </xs:element>
</xs:schema>
Community
  • 1
  • 1
Tu Nguyen
  • 33
  • 1
  • 7
  • advice_file doesn't name a type – Daniel A. White Jun 21 '16 at 17:13
  • Thanks Daniel for helping me edit my question. Actually advice_file is a complex type with all kind of detail inside it. It can generate the advice_file well as a class, but it completely ignores the second element vendorMerchID. – Tu Nguyen Jun 21 '16 at 17:16

1 Answers1

0

The reason you only get 1 element is because to be valid the xml must always have a single outer root node. What is suprising to me is that the second node was just ignored completely and no exception was thrown.

Your xsd represents the following xml

<advice_file/>

To have both elements, you need to write you xsd as follows:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <xs:element name="myRoot">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="advice_file" type="xs:string"/>
                <xs:element name="vendorMerchID" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

for the xml structure as:

<myRoot>
    <advice_file/>
    <vendorMerchID/>
</myRoot>

Alternatively, if your xml is like this:

<advice_file>
    <vendorMerchID/>
</advice_file>

Use the xsd:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <xs:element name="advice_file">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="vendorMerchID" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
Phil Blackburn
  • 1,047
  • 1
  • 8
  • 13
  • Thank you, Phil. Your solution works absolutely fine for me. However, the Xsd is provided from a partner of my company, I'm not sure if I modify the Xsd will cause any structure modification from their expected Xml. – Tu Nguyen Jun 24 '16 at 13:35
  • Receiving incorrect xsd is suprisingly common in my experience. In your situation I would be contacting the xsd 'owner' and have them correct it. They should appreciate being alerted to the error. When this has occured for me during development and when I know I can trust the xml content, I've edited my own copy of the xsd (and documented inline) so that I can create classes I need. Later, I can overwrite my copy with the master version of xsd once it is fixed. – Phil Blackburn Jun 24 '16 at 13:56