Altova Mailing List Archives>Archive Index >comp.text.xml Archive Home >Recent entries >Thread Prev - 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/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/
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
