Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


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

From: Martin Honnen <mahotrash@-----.-->
To: NULL
Date: 7/21/2009 12:18:00 PM
RayLopez99 wrote:
>  Also 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:

     class Program
     {
         static void Main(string[] args)
         {
             List<Foo> foos = new List<Foo>()
             {
                 new Foo() { Bar = "bar 1", Baz = 1 },
                 new Foo() { Bar = "bar 2", Baz = 2 },
                 new Foo() { Bar = "bar 3", Baz = 3 }
             };

             XmlWriterSettings xrs = new XmlWriterSettings();
             xrs.Indent = true;
             XmlSerializer ser = new XmlSerializer(typeof(Foo));
             using (XmlWriter xr = 
XmlWriter.Create(@"..\..\XMLSer1.xml", xrs))
             {
                 xr.WriteStartDocument();
                 xr.WriteStartElement("Root");
                 foreach (Foo foo in foos)
                 {
                     ser.Serialize(xr, foo);
                 }
                 xr.WriteEndElement();
                 xr.WriteEndDocument();
                 xr.Close();
             }

             List<Foo> foos2 = new List<Foo>();
             using (XmlReader xr = XmlReader.Create(@"..\..\XMLSer1.xml"))
             {
                 while (xr.Read())
                 {
                     if (xr.NodeType == XmlNodeType.Element && xr.Name 
== "Foo")
                     {
                         Foo foo = (Foo)ser.Deserialize(xr);
                         foos2.Add(foo);
                     }
                 }
                 xr.Close();
             }

             foreach (Foo foo in foos2)
             {
                 Console.WriteLine("Bar: {0}; Baz: {1}", foo.Bar, foo.Baz);
             }
         }
     }

     public class Foo
     {
         public string Bar { get; set; }
         public int Baz { get; set; }
     }


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 method.


-- 

	Martin Honnen
	http://msmvps.com/blogs/martin_honnen/


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