Good time.

there is XML like this

<?xml version="1.0" encoding="utf-8"?> <ArrayOfLibraryUnit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <LibraryUnit xsi:type="Book"> <NameVal>книга1</NameVal> </LibraryUnit> <LibraryUnit xsi:type="Book"> <NameVal>книга2</NameVal> </LibraryUnit> <LibraryUnit xsi:type="Newspaper"> <NameVal>Газета1</NameVal> <pageCountVal>5</pageCountVal> </LibraryUnit> </ArrayOfLibraryUnit> 

you need to write xsd for it ... I'm not strong in this business, I killed the next half day and gave birth to the next xsd

 <?xml version="1.0" encoding="utf-8"?> <xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified"> <xsd:element name="ArrayOfLibraryUnit" > <xsd:complexType> <xsd:sequence> <xsd:choice minOccurs="0" maxOccurs="unbounded"> <xsd:element name="LibraryUnit" type="Book"></xsd:element> <xsd:element name="LibraryUnit" type="Newspaper"></xsd:element> </xsd:choice> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:complexType name="Book"> <xsd:sequence> <xsd:element name="NameVal" type="xsd:string" /> </xsd:sequence> </xsd:complexType> <xsd:complexType name="Newspaper"> <xsd:sequence> <xsd:element name="NameVal" type="xsd:string" /> <xsd:element name="pageCountVal" type="xsd:unsignedByte" /> </xsd:sequence> </xsd:complexType> </xs:schema> 

but it only works if the xml contains only books (Book). Tell me how to properly?

all the difficulty is due to the fact that Book and Newspaper objects are inherited from LibraryUnit.

Thank!

    2 answers 2

    You Book and NewsPaper are indistinguishable for the validator. Therefore, the only way

     <?xml version="1.0" encoding="utf-8" ?> <xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified"> <xsd:element name="ArrayOfLibraryUnit"> <xsd:complexType> <xsd:sequence> <xsd:element name="LibraryUnit" type="LibraryUnitType" maxOccurs="unbounded" /> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:complexType name="LibraryUnitType"> <xsd:sequence> <xsd:element name="NameVal" type="xsd:string" /> <xsd:element name="pageCountVal" type="xsd:unsignedByte" minOccurs="0" /> </xsd:sequence> </xsd:complexType> </xs:schema> 

      Thanks for the answer. As a result, I redid a little serialization in xml.

      Initially, my parents had such attributes on the class.

       [XmlInclude(typeof(Book)), XmlInclude(typeof(Newspaper))] 

      did serialization through the array

       public class SerializeLibraryUnits { [XmlArrayItem(typeof(Book)), XmlArrayItem(typeof(Newspaper))] public LibraryUnit[] libraryUnits; } 

      as a result, xml became this kind

       <SerializeLibraryUnits xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <libraryUnits> <Book> <NameVal>книга1</NameVal> </Book> <Book> <NameVal>книга2</NameVal> </Book> <Newspaper> <NameVal>Газета3</NameVal> <pageCountVal>7</pageCountVal> </Newspaper> </libraryUnits> </SerializeLibraryUnits> 

      I study... ))