I have to parse a XML where the xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" namespace is missing, so the xml looks like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<program>
<scriptList>
<script type="StartScript">
<isUserScript>false</isUserScript>
</script>
</scriptList>
</program>
but should look like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<program xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<scriptList>
<script xsi:type="StartScript">
<isUserScript>false</isUserScript>
</script>
</scriptList>
</program>
The type attribute ensures the correct subclass e.g.
class StartScript : script
{...}
The parser is auto generated from an handwritten xsd via $> xsd.exe a.xsd /classes (.Net). Here is the xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="qualified">
<!-- Main element -->
<xs:element name="program">
<xs:complexType>
<xs:sequence>
<xs:element name="scriptList">
<xs:complexType>
<xs:sequence>
<xs:element name="script" type="script" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="script" />
<xs:complexType name="StartScript">
<xs:complexContent>
<xs:extension base="script">
<xs:all>
<xs:element name="isUserScript" type="xs:boolean"></xs:element>
</xs:all>
</xs:extension>
</xs:complexContent>
</xs:complexType>
A simple solution is to run a string-replace (" type=\"" to " xsi:type=\"") on the input XML but this is pretty ugly. Is there a better solution?
XmlSerializer
, or something else? Are there other types ofscript
or onlyStartScript
? – dbc