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/20/2009 11:40:00 AM
RayLopez99 wrote:

>             DecoratedPerson myDecPerson1 = new DecoratedPerson();
>             myDecPerson1.FirstName = "Rob1";
>             myDecPerson1.LastName = "Smith1";
>             myDecPerson1.Password = "SecretSmith";
>             myDecPerson1.Email = "Smith1@a...";
>             myDecPerson1.Age = int.Parse("99");
>             myDecPerson1.UserId = "Rob1Smith";
>             Guid myGuid = new Guid();
>             myGuid = System.Guid.NewGuid();
>             myDecPerson1.UserGuid = myGuid;
>             // add to List
>             myDecPersonList.Add(myDecPerson1);
> 
>             // now do second person, and add to list--fails!...
> 
>             DecoratedPerson myDecPerson2 = new DecoratedPerson();
> 
>             myDecPerson2.FirstName = "Rob2";
>             myDecPerson2.LastName = "Smith2";
>             myDecPerson2.Password = "SecretSmith2";
>             myDecPerson2.Email = "Smith2@a...";
>             myDecPerson2.Age = int.Parse("88");
>             myDecPerson2.UserId = "Rob2Smith";
>             myGuid = new Guid();
>             myGuid = System.Guid.NewGuid();
>             myDecPerson2.UserGuid = myGuid;
>             myDecPersonList.Add(myDecPerson2);
> 
>             //create file and add nodes ..
> 
>             string totalFilepath = Path.Combine
> (Request.PhysicalApplicationPath, @"App_Data
> \XMLPasswordDoc77.xml");  //create path
>             XmlSerializerNamespaces ns = new XmlSerializerNamespaces
> ();
> 
>             ns.Add("mytest", "http://www.w3.org/2001/XMLSchemaMyOwn");
> 
>              XmlWriterSettings settings = new XmlWriterSettings();
>              settings.OmitXmlDeclaration = false; // Don't remove the
> <?xml version="1.0" encoding="utf-8"?>
> 
>              XmlWriter writer = XmlWriter.Create(totalFilepath,
> settings);
> 
> 
>             XmlSerializer serializer = new XmlSerializer(typeof
> (DecoratedPerson));
> 
>             foreach (DecoratedPerson p in myDecPersonList)
>             {
> 
>                  serializer.Serialize(writer, p, ns); //only works w/o
> exception if only one element in list! if > 1 fails!
>             }
> 
>             writer.Close();
> 
> 
> 
> 
>         } //end of method

The .NET framework allows you to create an XML fragment by setting
   settings.ConformanceLevel = ConformanceLevel.Fragment;
on your XmlWriterSettings variable. That way you should be able to write 
out serveral DecoratedPerson elements. You should however be aware that 
you can't read the generated XML with XmlDocument or XDocument as these 
implement the W3C specification for well-formed XML documents to have 
exactly one root element that contains all other elements.
XmlReader on the other hand can also be set in "fragment mode" with an 
XmlReaderSettings instance and the same ConformanceLevel setting.

-- 

	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