3

The Usual way, i code to write a XML file is,

    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent = true;
    XmlWriter writer = XmlWriter.Create("Products.xml", settings);

    writer.WriteStartDocument();

    writer.WriteComment("This file is generated by the program.");

    writer.WriteStartElement("Product");
    writer.WriteAttributeString("ID", "001");
    writer.WriteAttributeString("Name", "Keyboard");
    writer.WriteElementString("Price", "10.00");
    writer.WriteStartElement("OtherDetails");
    writer.WriteElementString("BrandName", "X Keyboard");
    writer.WriteElementString("Manufacturer", "X Company");
    writer.WriteEndElement();
    writer.WriteEndDocument();
    writer.Flush();
    writer.Close();

But the above code gives me a different XML structure, How to code if i need the output as below given structure,

<Books>
<Book ISBN="0553212419">
<title>Sherlock Holmes</title>
<author>Sir Arthur Conan Doyle</author>
</Book>
<Book ISBN="0743273567">
<title>The Great Gatsby</title>
<author>F. Scott Fitzgerald</author>
</Book>
<Book ISBN="0684826976">
<title>Undaunted Courage</title>
<author>Stephen E. Ambrose</author>
</Book>
<Book ISBN="0743203178">
<title>Nothing Like It In the World</title>
<author>Stephen E. Ambrose</author>
</Book>
</Books>

Thanks

Anuya
  • 8,082
  • 49
  • 137
  • 222

2 Answers2

16

As was commented, just modify the code you already have to write the correct elements.

XmlWriter writer = XmlWriter.Create(@"Products.xml", settings);

writer.WriteStartDocument();

writer.WriteComment("This file is generated by the program.");

writer.WriteStartElement("Books");
writer.WriteStartElement("Book");
writer.WriteAttributeString("ISBN", "0553212419");
writer.WriteElementString("Title", "Sherlock Holmes");
writer.WriteElementString("Author", "Sir Arthur Conan Doyle");
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
writer.Close();

Wash, rinse, repeat. I would recommend writing a method to add each book.

EDIT - method for writing books

void WriteBookData(XmlWriter writer, string isbn, string title, string author)
{
    writer.WriteStartElement("Book");
    writer.WriteAttributeString("ISBN", isbn);
    writer.WriteElementString("Title", title);
    writer.WriteElementString("Author", author);
    writer.WriteEndElement();
}

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
using (XmlWriter writer = XmlWriter.Create(@"Products.xml", settings))
{
    writer.WriteStartDocument();

    writer.WriteComment("This file is generated by the program.");

    writer.WriteStartElement("Books");
    WriteBookData(writer, "0553212419", "Sherlock Holmes", "Sir Arthur Conan Doyle");
    writer.WriteEndElement();
    writer.WriteEndDocument();
    writer.Flush();
}
pstrjds
  • 16,840
  • 6
  • 52
  • 61
4

Why don't you use Linq to XML .Its pretty straightforward and very easy to undestand

   string filename = string.Empty;
 XElement res= new XElement ("Books",new XElement ("Book",new XAttribute   ("ISBN","="+055321212),
            new XElement ("Title","Sherlock Homes"),
            new XElement ("author","Sir Arthur Conan")
            )
            );


        res.Save(filename);

you can use foreach look to store the hard coded values in the above statements

praveen
  • 12,083
  • 1
  • 41
  • 49