XmlDeserialize problem

M

m96

hi,

i have a problem with XmlDeserilisation which drives me crazy. i know i
must be missing something very trivial...

i'm trying to parse the folling xml file

i really would appreciate any help.

cheers...

===
<?xml version="1.0" ?>
<courses>
<course>
<title>title1</title>
<remoteuri>remuri</remoteuri>
<localuri>locuri</localuri>
<overwrite>pdf</overwrite>
</course>
<course>
<title>test2</title>
<remoteuri>renuri</remoteuri>
<user>username</user>
</course>
</courses>
===

with this code


===
using System;
using System.Collections;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

[XmlRoot("courses")]
public class MeList {
public course[] courses;
}

public class course {
public string title;
public string remoteuri;
public string localuri;
public string overwrite;
}

public class Run
{
public static void Main()
{
Run test = new Run();
test.DeserializeObject("ad.xml");
}

public void DeserializeObject(string filename)
{
FileStream fs = new FileStream(filename, FileMode.Open);
XmlSerializer x = new XmlSerializer(typeof(MeList));
MeList m = (MeList) x.Deserialize(fs);
Console.WriteLine("Members:");

Console.WriteLine("..{0}", m.courses.Length);
foreach(course c in m.courses) { Console.WriteLine("....\t"
+ c.overwrite);}
}
}
====
 
J

John Sun

I think you have to declare [Serializable] in MeList, by the way,
what exact error did you get?

Jianwei
 
M

m96

the error i get is the following:

Unhandled Exception: System.NullReferenceException: Object reference not
set to an instance of an object.
at Run.DeserializeObject(String filename) in c:\Documents and
Settings\Administrator\My Documents\tmp\test.cs:line 34
at Run.Main() in c:\Documents and Settings\Administrator\My
Documents\tmp\test.cs:line 24

that means also (what i can see while debugging) that MeList.courses is
empty. that again means i'm declaring the objects somehow wrong so they
don't get filled by XmlDeserializer.

that prooves again i still haven't understood how that xmlserializer
works... argh.

thank you for the fast answer...




John said:
I think you have to declare [Serializable] in MeList, by the way,
what exact error did you get?

Jianwei
hi,

i have a problem with XmlDeserilisation which drives me crazy. i know
i must be missing something very trivial...

i'm trying to parse the folling xml file

i really would appreciate any help.

cheers...

===
<?xml version="1.0" ?>
<courses>
<course>
<title>title1</title>
<remoteuri>remuri</remoteuri>
<localuri>locuri</localuri>
<overwrite>pdf</overwrite>
</course>
<course>
<title>test2</title>
<remoteuri>renuri</remoteuri>
<user>username</user>
</course>
</courses>
===

with this code


===
using System;
using System.Collections;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

[XmlRoot("courses")]
public class MeList {
public course[] courses;
}

public class course {
public string title;
public string remoteuri;
public string localuri;
public string overwrite;
}

public class Run
{
public static void Main()
{
Run test = new Run();
test.DeserializeObject("ad.xml");
}

public void DeserializeObject(string filename)
{
FileStream fs = new FileStream(filename, FileMode.Open);
XmlSerializer x = new XmlSerializer(typeof(MeList));
MeList m = (MeList) x.Deserialize(fs);
Console.WriteLine("Members:");

Console.WriteLine("..{0}", m.courses.Length);
foreach(course c in m.courses) { Console.WriteLine("....\t"
+ c.overwrite);}
}
}
====
 
J

John Sun

m96 said:
the error i get is the following:

Unhandled Exception: System.NullReferenceException: Object reference not
set to an instance of an object.
at Run.DeserializeObject(String filename) in c:\Documents and
Settings\Administrator\My Documents\tmp\test.cs:line 34
at Run.Main() in c:\Documents and Settings\Administrator\My
Documents\tmp\test.cs:line 24

that means also (what i can see while debugging) that MeList.courses is
empty. that again means i'm declaring the objects somehow wrong so they
don't get filled by XmlDeserializer.

that prooves again i still haven't understood how that xmlserializer
works... argh.

thank you for the fast answer...




John said:
I think you have to declare [Serializable] in MeList, by the way,
what exact error did you get?

Jianwei
hi,

i have a problem with XmlDeserilisation which drives me crazy. i know
i must be missing something very trivial...

i'm trying to parse the folling xml file

i really would appreciate any help.

cheers...

===
<?xml version="1.0" ?>
<courses>
<course>
<title>title1</title>
<remoteuri>remuri</remoteuri>
<localuri>locuri</localuri>
<overwrite>pdf</overwrite>
</course>
<course>
<title>test2</title>
<remoteuri>renuri</remoteuri>
<user>username</user>
</course>
</courses>
===

with this code


===
using System;
using System.Collections;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

[XmlRoot("courses")]
public class MeList {
public course[] courses;
}

public class course {
public string title;
public string remoteuri;
public string localuri;
public string overwrite;
}

public class Run
{
public static void Main()
{
Run test = new Run();
test.DeserializeObject("ad.xml");
}

public void DeserializeObject(string filename)
{
FileStream fs = new FileStream(filename, FileMode.Open);
XmlSerializer x = new XmlSerializer(typeof(MeList));
MeList m = (MeList) x.Deserialize(fs);
Console.WriteLine("Members:");

Console.WriteLine("..{0}", m.courses.Length);
foreach(course c in m.courses) {
Console.WriteLine("....\t" + c.overwrite);}
}
}
====
Be honest to you, I am not sure how you can deserialize into the array,
what I will do will be loop through each node in the xml file, and
deserilize course object. Apply [Serializable] attribute on the course.

HTH

Jianwei
 
M

m96

found the solution; in case someone is interested in it.

xsd ad.xml
xsd /l:CS /c ad.xsd

generates the classes with which you can deserialize. actually very nice
and handy tool.

cheers.


John said:
m96 said:
the error i get is the following:

Unhandled Exception: System.NullReferenceException: Object reference
not set to an instance of an object.
at Run.DeserializeObject(String filename) in c:\Documents and
Settings\Administrator\My Documents\tmp\test.cs:line 34
at Run.Main() in c:\Documents and Settings\Administrator\My
Documents\tmp\test.cs:line 24

that means also (what i can see while debugging) that MeList.courses
is empty. that again means i'm declaring the objects somehow wrong so
they don't get filled by XmlDeserializer.

that prooves again i still haven't understood how that xmlserializer
works... argh.

thank you for the fast answer...




John said:
I think you have to declare [Serializable] in MeList, by the way,
what exact error did you get?

Jianwei

m96 wrote:

hi,

i have a problem with XmlDeserilisation which drives me crazy. i
know i must be missing something very trivial...

i'm trying to parse the folling xml file

i really would appreciate any help.

cheers...

===
<?xml version="1.0" ?>
<courses>
<course>
<title>title1</title>
<remoteuri>remuri</remoteuri>
<localuri>locuri</localuri>
<overwrite>pdf</overwrite>
</course>
<course>
<title>test2</title>
<remoteuri>renuri</remoteuri>
<user>username</user>
</course>
</courses>
===

with this code


===
using System;
using System.Collections;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

[XmlRoot("courses")]
public class MeList {
public course[] courses;
}

public class course {
public string title;
public string remoteuri;
public string localuri;
public string overwrite;
}

public class Run
{
public static void Main()
{
Run test = new Run();
test.DeserializeObject("ad.xml");
}

public void DeserializeObject(string filename)
{
FileStream fs = new FileStream(filename, FileMode.Open);
XmlSerializer x = new XmlSerializer(typeof(MeList));
MeList m = (MeList) x.Deserialize(fs);
Console.WriteLine("Members:");

Console.WriteLine("..{0}", m.courses.Length);
foreach(course c in m.courses) {
Console.WriteLine("....\t" + c.overwrite);}
}
}
====

Be honest to you, I am not sure how you can deserialize into the array,
what I will do will be loop through each node in the xml file, and
deserilize course object. Apply [Serializable] attribute on the course.

HTH

Jianwei
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top