Altova Mailing List Archives>Archive Index >comp.text.xml Archive Home >Recent entries >Thread Prev - Re: Append to a file using XML Serialization? Easy way to do this? >Thread Next - Re: Append to a file using XML Serialization? Easy way to do this? Re: Append to a file using XML Serialization? Easy way to do this?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
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
