Reputation: 901
I have an XSD:
<xs:complexType name="rootType">
<xs:sequence>
<xs:element name="foo" type="fooType" minOccurs="1" maxOccurs="unbounded"/>
<xs:element name="bar" type="barType" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<!-- SKIPPED -->
<xs:element name="root" type="rootType"></xs:element>
I have an XML built using this XSD:
<?xml version="1.0" encoding="utf-8"?>
<root>
<foo><!-- SKIPPED --></foo>
<foo><!-- SKIPPED --></foo>
<foo><!-- SKIPPED --></foo>
<bar><!-- SKIPPED --></bar>
<bar><!-- SKIPPED --></bar>
<bar><!-- SKIPPED --></bar>
<bar><!-- SKIPPED --></bar>
</root>
Now I want to serialize/deserialize this XML using XmlSerializer. I have C# classes:
public class fooType
{
public string element { get; set; }
}
public class barType
{
public string element { get; set; }
}
public class rootType
{
public fooType[] foos { get; set; }
public barType[] bars { get; set; }
}
There were some XML-related attributes, like XmlElementAttribute, but I omit them in example above for simplicity.
Now please take a look at rootType
class definition. Here we have two properties foos
and bars
. They will be serialized like root elements for fooType[]
and barType[]
arrays:
<?xml version="1.0" encoding="utf-8"?>
<root>
<foos>
<foo><!-- SKIPPED --></foo>
<foo><!-- SKIPPED --></foo>
<foo><!-- SKIPPED --></foo>
</foos>
<bars>
<bar><!-- SKIPPED --></bar>
<bar><!-- SKIPPED --></bar>
<bar><!-- SKIPPED --></bar>
<bar><!-- SKIPPED --></bar>
</bars>
</root>
But this is not what I want. How to serialize them according to XSD and example at the beginning of this post?
Upvotes: 0
Views: 565
Reputation: 31071
I find that the quickest way of working out how to design your classes to match a specific schema is to run the xsd.exe
tool backwards. Tell it to generate classes from your schema, and compare them with your hand-written classes to see where you went wrong. It's a great way of gaining experience in how the serializer works.
Upvotes: 1
Reputation: 54638
Based on your criteria, I would Implement IXmlSerializable
on rootType
. There is a great read here on SO on how to create the XML you are looking for.
Proper way to implement IXmlSerializable?
Upvotes: 1