Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Append to a file using XML Serialization? Easy way to do this?

From: RayLopez99 <raylopez88@-----.--->
To: NULL
Date: 7/22/2009 4:54:00 PM
On Jul 21, 6:18=A0am, Martin Honnen <mahotr...@yahoo.de> wrote:
> RayLopez99 wrote:
> > =A0Also does anybody know if there is a 'easier' (or cleaner, less code
> > required) way of deserializing than checking each node as per the
> > below code?
>
> Yes, certainly, see this example:
>
> =A0 =A0 =A0class Program
> =A0 =A0 =A0{
> =A0 =A0 =A0 =A0 =A0static void Main(string[] args)
> =A0 =A0 =A0 =A0 =A0{
> =A0 =A0 =A0 =A0 =A0 =A0 =A0List<Foo> foos =3D new List<Foo>()
> =A0 =A0 =A0 =A0 =A0 =A0 =A0{
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0new Foo() { Bar =3D "bar 1", Baz =3D 1=
 },
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0new Foo() { Bar =3D "bar 2", Baz =3D 2=
 },
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0new Foo() { Bar =3D "bar 3", Baz =3D 3=
 }
> =A0 =A0 =A0 =A0 =A0 =A0 =A0};
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0XmlWriterSettings xrs =3D new XmlWriterSetting=
s();
> =A0 =A0 =A0 =A0 =A0 =A0 =A0xrs.Indent =3D true;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0XmlSerializer ser =3D new XmlSerializer(typeof=
(Foo));
> =A0 =A0 =A0 =A0 =A0 =A0 =A0using (XmlWriter xr =3D
> XmlWriter.Create(@"..\..\XMLSer1.xml", xrs))
> =A0 =A0 =A0 =A0 =A0 =A0 =A0{
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0xr.WriteStartDocument();
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0xr.WriteStartElement("Root");
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0foreach (Foo foo in foos)
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0{
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0ser.Serialize(xr, foo);
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0}
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0xr.WriteEndElement();
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0xr.WriteEndDocument();
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0xr.Close();
> =A0 =A0 =A0 =A0 =A0 =A0 =A0}
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0List<Foo> foos2 =3D new List<Foo>();
> =A0 =A0 =A0 =A0 =A0 =A0 =A0using (XmlReader xr =3D XmlReader.Create(@"..\=
..\XMLSer1.xml"))
> =A0 =A0 =A0 =A0 =A0 =A0 =A0{
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0while (xr.Read())
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0{
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0if (xr.NodeType =3D=3D XmlNode=
Type.Element && xr.Name
> =3D=3D "Foo")
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0{
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Foo foo =3D (Foo)ser.D=
eserialize(xr);
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0foos2.Add(foo);
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0}
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0}
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0xr.Close();
> =A0 =A0 =A0 =A0 =A0 =A0 =A0}
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0foreach (Foo foo in foos2)
> =A0 =A0 =A0 =A0 =A0 =A0 =A0{
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Console.WriteLine("Bar: {0}; Baz: {1}"=
, foo.Bar, foo.Baz);
> =A0 =A0 =A0 =A0 =A0 =A0 =A0}
> =A0 =A0 =A0 =A0 =A0}
> =A0 =A0 =A0}
>
> =A0 =A0 =A0public class Foo
> =A0 =A0 =A0{
> =A0 =A0 =A0 =A0 =A0public string Bar { get; set; }
> =A0 =A0 =A0 =A0 =A0public int Baz { get; set; }
> =A0 =A0 =A0}
>
> So you simply read through the file with an XmlReader's
> while(reader.Read()) loop and each time the reader is positioned on an
> element you are interested in you pass the reader to the Deserialize meth=
od.
>
> --

I thank you for your time.

However, I found a mistake in your code that I cannot overcome.

Here it is: =A0

if (xr.NodeType =3D=3D XmlNodeType.Element && xr.Name  =3D=3D "Foo")

I believe what happens here, from what I can tell, is that  xr.Name
=3D=3D "Foo" is never triggered (never true).  What you get is xr.Name =3D=
=3D
Bar or xr.Name =3D=3DBaz.

Can you double check this?  If in fact it's what I find, this is
identical to the "long" code I posted, which is a series of switch/
case statements, which is no shorter than the code I posted in my
previous post.

I will try again with your code tommorrow.

RL



transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent