private void XmlWriteTest () {string fileName = "C: //Test.xml";. XmlTextWriter writer = new XmlTextWriter (fileName, null); // Use indenting for readability writer.Formatting = Formatting.Indented; writer.WriteComment ( " Sample XML fragment "); // Write an element (this one is the root). Writer.writestartElement (" BookStore ";
// Write the namespace declaration. Writer.writeAttributeString ("XMLns", "BK", NULL, "URN: SAMPLES");
Writer.writestartElement ("book");
// Lookup the prefix and then write the ISBN attribute string prefix = writer.LookupPrefix. ( "Urn: samples"); writer.WriteStartAttribute (prefix, "ISBN", "urn: samples"); writer.WriteString ( "1- 861003-78 "); Writer.writeEndattribute ();
// Write the title. Writer.writestrtlement ("Title"); Writer.writestring ("The Handmaid's Tale"); Writer.WriteEndelement (); // Write The Price. Writer.WriteElementsTRING ("Price", "19.95") // WRITE The Style Element. Writer.writestartElement (Prefix, "STYLE", "URN: Samples"); Writer.WritString ("Hardcover"); Writer.writeEndelement ();
// Write the end tag for the book element. Writer.writeEndelement ();
// Write the close tag for the root element. Writer.writeEndelement (); // Write the xml to file and close the Writer. Writer.flush (); Writer.close ();
. // Read the file back in and parse to ensure well formed XML XmlDocument doc = new XmlDocument ();. // Preserve white space for readability doc.PreserveWhitespace = true; // Load the file doc.Load (fileName); / / WRITE THE XML Content To The Console. MessageBox.show (Doc.innerXML);