Pages

Monday, September 2, 2013

Create XML manually using XML writer



 XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        using (XmlWriter writer = XmlWriter.Create(MapPath("Products.xml"), settings))
        {
            writer.WriteStartDocument();
            writer.WriteComment("This file is generated by the program.");
            writer.WriteStartElement("Books");

//if u want  foreach()    
             writer.WriteStartElement("Book");
            writer.WriteAttributeString("ISBN",
"0553212419");
            writer.WriteElementString("Title", "
Sherlock Holmes");
            writer.WriteElementString("Author", "
Sir Arhur Conan Doyle");
            writer.WriteEndElement();

//end foreach
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();
        }


It Creates "Products.xml" file as a output,



Product.xml
<?xml version="1.0" encoding="utf-8"?>
<!--This file is generated by the program.-->
<Books>
  <Book ISBN="0553212419">
    <Title>Sherlock Holmes</Title>
    <Author>Sir Arhur Conan Doyle</Author>
  </Book>
</Books>

No comments:

Post a Comment