Connecting Tech Pros Worldwide Help | Site Map

XML and C#

Boris
Guest
 
Posts: n/a
#1: Nov 15 '05
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.


Nicholas Paldino [.NET/C# MVP]
Guest
 
Posts: n/a
#2: Nov 15 '05

re: XML and C#


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.


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com

"Boris" <boris@portal.co.yu> wrote in message
news:ehG%23G2UyDHA.4064@tk2msftngp13.phx.gbl...[color=blue]
> 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.
>
>[/color]


Boris
Guest
 
Posts: n/a
#3: Nov 15 '05

re: XML and C#


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.




Doug
Guest
 
Posts: n/a
#4: Nov 15 '05

re: XML and C#


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" <boris@portal.co.yu> wrote in message
news:O0HKk9YyDHA.2000@TK2MSFTNGP11.phx.gbl...[color=blue]
> Thanks Nicholas,
> but now i have another problem. When i serialize my ArrayList that is[/color]
having[color=blue]
> 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[/color]
Studio[color=blue]
> Projects/ConsoleApplication38/bin/Debug/osoba.xml'.
>
> After i had opened my xml file i saw that every object from ArrayList[/color]
makes[color=blue]
> 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.
>
>
>
>[/color]


Marco Martin
Guest
 
Posts: n/a
#5: Nov 15 '05

re: XML and C#


Doug,

Why be so rude?
If you're having a bad day, dont bother answering questions.
"Doug" <doug@home.org> wrote in message
news:5H1Gb.7895$Q%5.7324@nwrddc01.gnilink.net...[color=blue]
> 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[/color]
a[color=blue]
> deal.
>
> sheesh.
>
> "Boris" <boris@portal.co.yu> wrote in message
> news:O0HKk9YyDHA.2000@TK2MSFTNGP11.phx.gbl...[color=green]
> > Thanks Nicholas,
> > but now i have another problem. When i serialize my ArrayList that is[/color]
> having[color=green]
> > 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[/color]
> Studio[color=green]
> > Projects/ConsoleApplication38/bin/Debug/osoba.xml'.
> >
> > After i had opened my xml file i saw that every object from ArrayList[/color]
> makes[color=green]
> > 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.
> >
> >
> >
> >[/color]
>
>[/color]


Boris
Guest
 
Posts: n/a
#6: Nov 15 '05

re: XML and C#


It was only example. I'am working owith much more bigger list.
"Doug" <doug@home.org> wrote in message
news:5H1Gb.7895$Q%5.7324@nwrddc01.gnilink.net...[color=blue]
> 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[/color]
a[color=blue]
> deal.
>
> sheesh.
>
> "Boris" <boris@portal.co.yu> wrote in message
> news:O0HKk9YyDHA.2000@TK2MSFTNGP11.phx.gbl...[color=green]
> > Thanks Nicholas,
> > but now i have another problem. When i serialize my ArrayList that is[/color]
> having[color=green]
> > 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[/color]
> Studio[color=green]
> > Projects/ConsoleApplication38/bin/Debug/osoba.xml'.
> >
> > After i had opened my xml file i saw that every object from ArrayList[/color]
> makes[color=green]
> > 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.
> >
> >
> >
> >[/color]
>
>[/color]


Nicholas Paldino [.NET/C# MVP]
Guest
 
Posts: n/a
#7: Nov 15 '05

re: XML and C#


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]
- mvp@spam.guard.caspershouse.com

"Boris" <boris@portal.co.yu> wrote in message
news:O0HKk9YyDHA.2000@TK2MSFTNGP11.phx.gbl...[color=blue]
> Thanks Nicholas,
> but now i have another problem. When i serialize my ArrayList that is[/color]
having[color=blue]
> 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[/color]
Studio[color=blue]
> Projects/ConsoleApplication38/bin/Debug/osoba.xml'.
>
> After i had opened my xml file i saw that every object from ArrayList[/color]
makes[color=blue]
> 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.
>
>
>
>[/color]


Boris
Guest
 
Posts: n/a
#8: Nov 15 '05

re: XML and C#


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/tut...utorial_id=236

Thank you.

"Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com> wrote in
message news:O7gfsViyDHA.2448@TK2MSFTNGP12.phx.gbl...[color=blue]
> Boris,
>
> Are you iterating through each element of the ArrayList and[/color]
serializing[color=blue]
> 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]
> - mvp@spam.guard.caspershouse.com
>
> "Boris" <boris@portal.co.yu> wrote in message
> news:O0HKk9YyDHA.2000@TK2MSFTNGP11.phx.gbl...[color=green]
> > Thanks Nicholas,
> > but now i have another problem. When i serialize my ArrayList that is[/color]
> having[color=green]
> > 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[/color]
> Studio[color=green]
> > Projects/ConsoleApplication38/bin/Debug/osoba.xml'.
> >
> > After i had opened my xml file i saw that every object from ArrayList[/color]
> makes[color=green]
> > 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.
> >
> >
> >
> >[/color]
>
>[/color]


Closed Thread