473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

XmlDeserialize problem

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);}
}
}
====
Nov 17 '05 #1
4 3762
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);}
}
}
====

Nov 17 '05 #2
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 Sun wrote:
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);}
}
}
====

Nov 17 '05 #3
m96 wrote:
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 Sun wrote:
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
Nov 17 '05 #4
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 Sun wrote:
m96 wrote:
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 Sun wrote:
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

Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bruce Davis | last post by:
I'm having a problem on windows (both 2000 and XP) with a multi-threaded tkinter gui application. The problem appears to be a deadlock condition when a child thread pops up a Pmw dialog window in...
11
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class...
0
by: Refky Wahib | last post by:
Hi I need Technical Support I finished a Great project using .Net and SQL Server and .Net Mobile Control My Business case is to implement this Program to accept about 1 Million concurrent...
117
by: Peter Olcott | last post by:
www.halting-problem.com
0
by: Bryan Ax | last post by:
I have an object that is being serialized via the XmlSerializer and stored in a database in xml format. Since initial creation, some of the properties of the object have changed (I have removed a...
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.