469,626 Members | 1,065 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,626 developers. It's quick & easy.

Problems with Deserialization...

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
Nov 12 '05 #1
1 1365
Linus wrote:
Hi,

I'm having problems with some very simple deserialization code
and would appreciate it very much if I could get some help here.
while (vr.Read()){}
XmlSerializer xs = new XmlSerializer(typeof(Event));
ev = (Event)xs.Deserialize(reader); System.Console.WriteLine(ev.Config.User); <<<< This is where it bombs


1. I do not understand what for "while (vr.Read()){}"
2. After 1) your stream position will point at the end of file, right?
So what the object do you expect to be read at the eof() position?
Probably it returns null, that successfuly is casted to Event class.
I suppose that "bombs" means "null reference exception"?

Vadim Chekan.

Nov 12 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Snowman | last post: by
3 posts views Thread by AnkitAsDeveloper [Ankit] | last post: by
3 posts views Thread by parrot toes | last post: by
5 posts views Thread by frustratedWithDotNet | last post: by
5 posts views Thread by Greg Allen | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.