Hi,
I'm having problems with some very simple deserialization code
and would appreciate it very much if I could get some help here.
The following is the code:
================================================== =======
XmlReader reader = new XmlTextReader("test.xml");
XmlValidatingReader vr = new XmlValidatingReader(reader);
try
{
while (vr.Read()){}
XmlSerializer xs = new XmlSerializer(typeof(Event));
ev = (Event)xs.Deserialize(reader);
}
catch (Exception e)
{
System.Console.WriteLine(e.Message);
}
System.Console.WriteLine(ev.Config.User); <<<< This is where it bombs
================================================== ========
My XML and XSD:
================================================== ====
<?xml version="1.0" encoding="utf-8"?>
<Event xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="C:\Web400\contract\ test.xsd">
<Config>
<Ticket>1</Ticket>
<User>Smith</User>
</Config>
</Event>
================================================== ===
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="Event">
<xs:complexType>
<xs:sequence>
<xs:element name="Config">
<xs:complexType>
<xs:sequence>
<xs:element name="Ticket" type="xs:int"/>
<xs:element name="User" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
================================================== ============
The only way I can make my XML look like this:
<?xml version="1.0" encoding="utf-8"?>
<Event xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="C:\Web400\contract\ test.xsd">
<Config xmlns="http://tempuri.org">
<Ticket>1</Ticket>
<User>Smith</User>
</Config>
</Event>
but the problem is I'll have to comment out the code that
validates the XML against the schema since the above XML won't pass.
What am I missing here?
-Linus