XML and C#

  • Thread starter Thread starter Boris
  • Start date Start date
B

Boris

I'm writting a console application and i want to store my ArrayList of
objects into XML document. Is there any way i can do that ?

Greets.
 
Boris,

There are two ways you can do this. The first is to use an instance of
the SoapFormatter class to create a SOAP representation of the arraylist and
its contents. Assuming that all of the items in the ArrayList support
serialization, this should work.

The second way is to use an instance of the XmlSerializer class. This
will produce "cleaner" (a relative term) XML, but it only serializes the
public properties of the instance. If the class isn't designed properly,
then this can cause some problems.

Hope this helps.
 
Thanks Nicholas,
but now i have another problem. When i serialize my ArrayList that is having
more than one object i am getting this error message in IE:



XML document must have a top level element. Error processing resource
'file:///D:/Documents and Settings/Administrator/My Documents/Visual Studio
Projects/ConsoleApplication38/bin/Debug/osoba.xml'.

After i had opened my xml file i saw that every object from ArrayList makes
one top level element and file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<NewDataSet xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<name>Mike</name>
<surname>Jones</surname>
</NewDataSet>
<?xml version="1.0" encoding="utf-8"?>
<NewDataSet xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<name>John</name>
<surname>Doe</surname>
</NewDataSet>

How can serialize ArrayList inside one <NewDataSet> ... </NewDataSet> ?

Greets.
 
For crying out loud, if your array elements are only <name> and <surname>,
walk through the list and barf out the XML yourself. It ain't that big of a
deal.

sheesh.
 
Boris,

Are you iterating through each element of the ArrayList and serializing
them? It appears that is what you are doing. You want to change this so
that you serialize only the ArrayList itself, not the elements in it.
 
Yes. I did that. But when i try to serialize whole list i got an error.
I tried to find solution and something closest to it is at:
http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=236

Thank you.

Nicholas Paldino said:
Boris,

Are you iterating through each element of the ArrayList and serializing
them? It appears that is what you are doing. You want to change this so
that you serialize only the ArrayList itself, not the elements in it.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Boris said:
Thanks Nicholas,
but now i have another problem. When i serialize my ArrayList that is having
more than one object i am getting this error message in IE:



XML document must have a top level element. Error processing resource
'file:///D:/Documents and Settings/Administrator/My Documents/Visual Studio
Projects/ConsoleApplication38/bin/Debug/osoba.xml'.

After i had opened my xml file i saw that every object from ArrayList makes
one top level element and file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<NewDataSet xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<name>Mike</name>
<surname>Jones</surname>
</NewDataSet>
<?xml version="1.0" encoding="utf-8"?>
<NewDataSet xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<name>John</name>
<surname>Doe</surname>
</NewDataSet>

How can serialize ArrayList inside one <NewDataSet> ... </NewDataSet> ?

Greets.
 
Back
Top