Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

appending an xml file

Status
Not open for further replies.

litton1

Technical User
Apr 21, 2005
584
GB
hi all, just having a play with xml and c# before putting them to some real work :D. I have been using a book that shows how to use XmlSerializer to create an xml object then write that object to file. The code is as follows
Code:
// (1) Create array of objects to be serialized
            Movies[] films = {new Movies(5,"Citizen Kane","Orson Welles",

                             "Y", 1941,1 ),

                  new Movies(6,"Casablanca","Michael Curtiz",

                             "Y", 1942,2), new Movies(10,"Lord Of The Rings","Gandelf",

                             "Y", 2005,1) };

            // (2) Create serializer
            //     This attribute is used to assign name to XML root element 
            xRoot = new XmlRootAttribute();
            xRoot.ElementName = "films";
            xRoot.Namespace = "[URL unfurl="true"]http://www.myxml.com";[/URL]
            xRoot.IsNullable = true;

            // Specify that an array of movies types is to be serialized
            XmlSerializer xSerial = new XmlSerializer(typeof(Movies[]), xRoot);
            string filename = @"c:\oscarwinner_xml\oscarwinners.xml";

            // (3) Stream to write XML into
            TextWriter writer = new StreamWriter(filename);
            xSerial.Serialize(writer, films);

This works fine but the book does not then tell you how to add another object to the xml file? So i tried..
Code:
 Movies mvs = new Movies(ID, mTitle, mDirector, mbestPicture, mYear, rank);

                    // Specify that an array of movies types is to be serialized
                    XmlSerializer xSerial = new XmlSerializer(typeof(Movies), xRoot);
                    string filename = @"c:\oscarwinner_xml\oscarwinners.xml";

                    // (3) Stream to write XML into
                    TextWriter writer = new StreamWriter(filename, true);
                    
                    xSerial.Serialize(writer, mvs);
[code]

unfortunately this method also adds another top level node (films) before and after the record. So... how do I add another object to the xml file? Or do i just overwrite the file?
Any help appreciated.


Age is a consequence of experience
 
You may be forced to call other classes into the fray like the StringWriter and the XmlDocument (though you can approach it a number of different ways if this doesn't suite your fancy):

Code:
XmlSerializer serializer = new XmlSerializer("whatever");
                
System.IO.StringWriter stringWriter = new System.IO.StringWriter();

serializer.Serialize(stringWriter, myObject);

string serializedObject = stringWriter.GetStringBuilder().ToString();

The code above, well, it will probably break, but once you get it fixed, you'll have your serialized object XML in string form. From there you can do something like building and XmlDocument from the existing file, then appending a child node in the proper place (setting the OuterXml property of the new node to your string minus the XML headings).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top