XML Example

E

eric

Any examples of creating an xml document in code completely from scratch?
Every example I've seen loads a document from a pre-existing file. Nothing
difficult something like
<?xml version="1.0" encoding="utf-8" ?>
<bookstore>
<book>
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book>
<title>War and War</title>
<author>Smith, John</author>
<price>15.95</price>
</book>

</bookstore>
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

eric said:
Any examples of creating an xml document in code completely from scratch?
Every example I've seen loads a document from a pre-existing file.

XmlDocument doc = new XmlDocument();
XmlNode root = doc.CreateElement("all");
doc.AppendChild(root);
for(int i = 0; i < 5; i++)
{
XmlNode elm = doc.CreateElement("one");
elm.AppendChild(doc.CreateTextNode("Element #" + (i + 1)));
root.AppendChild(elm);
}
doc.InsertBefore(doc.CreateXmlDeclaration("1.0", "UTF-8",
"yes"), doc.DocumentElement);
doc.Save(@"C:\test2.xml");

Arne
 
A

Arto V Viitanen

eric kirjoitti:
Any examples of creating an xml document in code completely from scratch?
Every example I've seen loads a document from a pre-existing file. Nothing
difficult something like
<?xml version="1.0" encoding="utf-8" ?>
<bookstore>
<book>
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book>
<title>War and War</title>
<author>Smith, John</author>
<price>15.95</price>
</book>

</bookstore>

using System;
using System.Xml;

try {
using (XmlWriter writer = XmlWriter.Create("books.xml")) {
writer.WriteStartElement("bookstore");
writer.WriteStartElement("book");
writer.WriteElementString("title",Oberon's Legacy");
writer.WriteElementString("author","Corets, Eva");
writer.WriteElementString("price","5.95");
writer.WriteEndElement();
writer.WriteStartElement("book");
writer.WriteElementString("title","War and War");
writer.WriteElementString("author",Smith, John");
writer.WriteElementString("price","15.95");
writer.WriteEneElement();
writer.WriteEndElement();
writer.Flush();
}
} catch (Exception ex) {
System.Console.Error.WriteLine("Error writing xml file {0}, ex);
System.Environment.Exit(1);
}
 
C

Chris Shepherd

Arto said:
using System;
using System.Xml;

try {
using (XmlWriter writer = XmlWriter.Create("books.xml")) {
writer.WriteStartElement("bookstore");
writer.WriteStartElement("book");
writer.WriteElementString("title",Oberon's Legacy");
writer.WriteElementString("author","Corets, Eva");
writer.WriteElementString("price","5.95");
writer.WriteEndElement();
writer.WriteStartElement("book");
writer.WriteElementString("title","War and War");
writer.WriteElementString("author",Smith, John");
writer.WriteElementString("price","15.95");
writer.WriteEneElement();
writer.WriteEndElement();
writer.Flush();
}
} catch (Exception ex) {
System.Console.Error.WriteLine("Error writing xml file {0}, ex);
System.Environment.Exit(1);
}

The XmlDoc approach is better than doing it this way IMO. If you add a
tag, and add children to it, it handles appropriately positioning the
end tag, whereas doing it this way would leave you doing (probably
needless) debugging.

Chris.
 
J

Jon Skeet [C# MVP]

The XmlDoc approach is better than doing it this way IMO. If you add a
tag, and add children to it, it handles appropriately positioning the
end tag, whereas doing it this way would leave you doing (probably
needless) debugging.

LINQ to XML is even nicer:

XElement element = new XElement("all",
from i in Enumerable.Range(0, 5)
select new XElement("one", "Element #"+(i+1));

(Use XDocument to include all the declaration stuff - I can't test
this from where I'm writing at the moment, unfortunately.)

Jon
 
E

Eric

Thank you, Arne.

Arne Vajhøj said:
XmlDocument doc = new XmlDocument();
XmlNode root = doc.CreateElement("all");
doc.AppendChild(root);
for(int i = 0; i < 5; i++)
{
XmlNode elm = doc.CreateElement("one");
elm.AppendChild(doc.CreateTextNode("Element #" + (i + 1)));
root.AppendChild(elm);
}
doc.InsertBefore(doc.CreateXmlDeclaration("1.0", "UTF-8", "yes"),
doc.DocumentElement);
doc.Save(@"C:\test2.xml");

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top