// Open an XML document.
System.Xml.XmlDocument myXmlDocument = new System.Xml.XmlDocument();
myXmlDocument.Load(Server.MapPath("books.xml"));
System.Xml.XmlNode myXmlNode = myXmlDocument.DocumentElement.FirstChild;
// Create a new XML element and populate its attributes
System.Xml.XmlElement myXmlElement = myXmlDocument.CreateElement("book");
myXmlElement.SetAttribute("genre", Server.HtmlEncode("Policial"));
myXmlElement.SetAttribute("publicationdate", Server.HtmlEncode("20-09-2008"));
myXmlElement.SetAttribute("ISBN", Server.HtmlEncode("1-861003-11-0"));
// Insert the new element into the XML tree and save
myXmlDocument.DocumentElement.InsertBefore(myXmlElement, myXmlNode);
// Create a new XML element and populate its attributes
System.Xml.XmlElement myXmlElement2 = myXmlDocument.CreateElement("title");
myXmlElement2.InnerText = Server.HtmlEncode("MarkTwain");
// Insert the new element into the XML tree and save
myXmlDocument.DocumentElement.InsertBefore(myXmlElement2, myXmlNode);
// Create a new XML element and populate its attributes
System.Xml.XmlElement myXmlElement3 = myXmlDocument.CreateElement("author");
System.Xml.XmlElement myXmlElement4 = myXmlDocument.CreateElement("first-name");
myXmlElement4.InnerText = Server.HtmlEncode("Jose");
myXmlDocument.DocumentElement.InsertBefore(myXmlElement4, myXmlNode);
myXmlDocument.DocumentElement.InsertBefore(myXmlElement3, myXmlNode);
// Insert the new element into the XML tree and save
myXmlDocument.Save(Server.MapPath("books.xml"));
I made a few tweaks to your code...you were very close:
// Open an XML document.
System.Xml.XmlDocument myXmlDocument = new System.Xml.XmlDocument();
myXmlDocument.Load(Server.MapPath("books.xml"));
System.Xml.XmlNode myXmlNodeFirstBook = myXmlDocument.DocumentElement.FirstChild;
// Create a Book element and populate its attributes
System.Xml.XmlElement XmlElementMyBook = myXmlDocument.CreateElement("book");
XmlElementMyBook.SetAttribute("genre", Server.HtmlEncode("Policial"));
XmlElementMyBook.SetAttribute("publicationdate", Server.HtmlEncode("20-09-2008"));
XmlElementMyBook.SetAttribute("ISBN", Server.HtmlEncode("1-861003-11-0"));
// Insert the new element into the XML tree under Catalog
myXmlDocument.DocumentElement.InsertBefore(XmlElementMyBook, myXmlNodeFirstBook);
// Create a new XML element and populate its attributes
System.Xml.XmlElement myXmlElement2 = myXmlDocument.CreateElement("title");
myXmlElement2.InnerText = Server.HtmlEncode("MarkTwain");
// Insert the new element under the node we created
XmlElementMyBook.AppendChild(myXmlElement2);
// Create new XML elements and populate attributes
System.Xml.XmlElement myXmlElement3 = myXmlDocument.CreateElement("author");
XmlElementMyBook.AppendChild(myXmlElement3);
System.Xml.XmlElement myXmlElement4 = myXmlDocument.CreateElement("first-name");
myXmlElement4.InnerText = Server.HtmlEncode("Jose");
XmlElementMyBook.AppendChild(myXmlElement4);
myXmlDocument.Save(Server.MapPath("books.xml"));
The 'bookstore' start tag on line 3 does not match the end tag of 'book'. Line 13, position 5.
Sorry i didn´t mention that i had the root element -> <bookstore>
<!-- This file represents a fragment of a book store inventory -->
<bookstore xmlns="http://example.books.com">
<book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<date>09-06-1956</date>
<samplechapters>1 3 4</samplechapters>
<price alternative="discount">5.99</price>
<price>8.99</price>
</book>
</bookstore>
I had a problem with the above code. It didn't create a child of a Author element like it's supposed to. I made some changes and it worked. Here is the code.
// Open an XML document.
System.Xml.XmlDocument myXmlDocument = new System.Xml.XmlDocument();
myXmlDocument.Load(@"yourPath\books1.xml");
System.Xml.XmlNode myXmlNodeFirstBook = myXmlDocument.DocumentElement.FirstChild;
// Create a Book element and populate its attributes
System.Xml.XmlElement XmlElementMyBook = myXmlDocument.CreateElement("book");
XmlElementMyBook.SetAttribute("genre", "Funny");
XmlElementMyBook.SetAttribute("publicationdate", "21-03-2009");
XmlElementMyBook.SetAttribute("ISBN", "1-12345-11-0");
// Insert the new element into the XML tree under Catalog
myXmlDocument.DocumentElement.InsertBefore(XmlElementMyBook, myXmlNodeFirstBook);
// Create a new child of the book element
System.Xml.XmlElement myXmlElement2 = myXmlDocument.CreateElement("title");
myXmlElement2.InnerText = "Earthing";
// Insert the new element under the node we created
XmlElementMyBook.AppendChild(myXmlElement2);
// Create a new child of the book element
System.Xml.XmlElement myXmlElement3 = myXmlDocument.CreateElement("author");
XmlElementMyBook.AppendChild(myXmlElement3);
// Create new child of the author element
System.Xml.XmlElement myXmlElement4 = myXmlDocument.CreateElement("first-name");
myXmlElement4.InnerText = "Clint";
myXmlElement3.AppendChild(myXmlElement4);
// Create new child of the author element
System.Xml.XmlElement myXmlElement5 = myXmlDocument.CreateElement("last-name");
myXmlElement5.InnerText = "Ober";
myXmlElement3.AppendChild(myXmlElement5);
studioc
Member
8 Points
54 Posts
Populate the bookstore.xml C#
Sep 19, 2008 11:35 AM|LINK
Hi guys.
i´m trying to populate the bookstore. xml
with this code:
// Open an XML document. System.Xml.XmlDocument myXmlDocument = new System.Xml.XmlDocument(); myXmlDocument.Load(Server.MapPath("books.xml")); System.Xml.XmlNode myXmlNode = myXmlDocument.DocumentElement.FirstChild; // Create a new XML element and populate its attributes System.Xml.XmlElement myXmlElement = myXmlDocument.CreateElement("book"); myXmlElement.SetAttribute("genre", Server.HtmlEncode("Policial")); myXmlElement.SetAttribute("publicationdate", Server.HtmlEncode("20-09-2008")); myXmlElement.SetAttribute("ISBN", Server.HtmlEncode("1-861003-11-0")); // Insert the new element into the XML tree and save myXmlDocument.DocumentElement.InsertBefore(myXmlElement, myXmlNode); // Create a new XML element and populate its attributes System.Xml.XmlElement myXmlElement2 = myXmlDocument.CreateElement("title"); myXmlElement2.InnerText = Server.HtmlEncode("MarkTwain"); // Insert the new element into the XML tree and save myXmlDocument.DocumentElement.InsertBefore(myXmlElement2, myXmlNode); // Create a new XML element and populate its attributes System.Xml.XmlElement myXmlElement3 = myXmlDocument.CreateElement("author"); System.Xml.XmlElement myXmlElement4 = myXmlDocument.CreateElement("first-name"); myXmlElement4.InnerText = Server.HtmlEncode("Jose"); myXmlDocument.DocumentElement.InsertBefore(myXmlElement4, myXmlNode); myXmlDocument.DocumentElement.InsertBefore(myXmlElement3, myXmlNode); // Insert the new element into the XML tree and save myXmlDocument.Save(Server.MapPath("books.xml"));What I got updated on my XML file is
SGWellens
All-Star
126033 Points
10311 Posts
Moderator
Re: Populate the bookstore.xml C#
Sep 19, 2008 12:10 PM|LINK
I made a few tweaks to your code...you were very close:
// Open an XML document. System.Xml.XmlDocument myXmlDocument = new System.Xml.XmlDocument(); myXmlDocument.Load(Server.MapPath("books.xml")); System.Xml.XmlNode myXmlNodeFirstBook = myXmlDocument.DocumentElement.FirstChild; // Create a Book element and populate its attributes System.Xml.XmlElement XmlElementMyBook = myXmlDocument.CreateElement("book"); XmlElementMyBook.SetAttribute("genre", Server.HtmlEncode("Policial")); XmlElementMyBook.SetAttribute("publicationdate", Server.HtmlEncode("20-09-2008")); XmlElementMyBook.SetAttribute("ISBN", Server.HtmlEncode("1-861003-11-0")); // Insert the new element into the XML tree under Catalog myXmlDocument.DocumentElement.InsertBefore(XmlElementMyBook, myXmlNodeFirstBook); // Create a new XML element and populate its attributes System.Xml.XmlElement myXmlElement2 = myXmlDocument.CreateElement("title"); myXmlElement2.InnerText = Server.HtmlEncode("MarkTwain"); // Insert the new element under the node we created XmlElementMyBook.AppendChild(myXmlElement2); // Create new XML elements and populate attributes System.Xml.XmlElement myXmlElement3 = myXmlDocument.CreateElement("author"); XmlElementMyBook.AppendChild(myXmlElement3); System.Xml.XmlElement myXmlElement4 = myXmlDocument.CreateElement("first-name"); myXmlElement4.InnerText = Server.HtmlEncode("Jose"); XmlElementMyBook.AppendChild(myXmlElement4); myXmlDocument.Save(Server.MapPath("books.xml"));My blog
studioc
Member
8 Points
54 Posts
Re: Populate the bookstore.xml C#
Sep 19, 2008 12:27 PM|LINK
After that appears an error:
The 'bookstore' start tag on line 3 does not match the end tag of 'book'. Line 13, position 5.
Sorry i didn´t mention that i had the root element -> <bookstore>
<!-- This file represents a fragment of a book store inventory --> <bookstore xmlns="http://example.books.com"> <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0"> <title>The Autobiography of Benjamin Franklin</title> <author> <first-name>Benjamin</first-name> <last-name>Franklin</last-name> </author> <date>09-06-1956</date> <samplechapters>1 3 4</samplechapters> <price alternative="discount">5.99</price> <price>8.99</price> </book> </bookstore>How can i resolve it?
Thanks four your attention.
SGWellens
All-Star
126033 Points
10311 Posts
Moderator
Re: Populate the bookstore.xml C#
Sep 19, 2008 01:14 PM|LINK
The XmlDocument control does not generate invalid XML.
Your document started out invalid, or you hand-edited it and made a mistake.
Try looking at the document with XmlNotepad:
http://www.microsoft.com/downloads/details.aspx?familyid=72d6aa49-787d-4118-ba5f-4f30fe913628&displaylang=en
My blog
studioc
Member
8 Points
54 Posts
Re: Populate the bookstore.xml C#
Sep 19, 2008 02:29 PM|LINK
kshoufer
Member
2 Points
1 Post
Re: Populate the bookstore.xml C#
Aug 15, 2012 06:48 PM|LINK
I had a problem with the above code. It didn't create a child of a Author element like it's supposed to. I made some changes and it worked. Here is the code.
// Open an XML document.
System.Xml.XmlDocument myXmlDocument = new System.Xml.XmlDocument();
myXmlDocument.Load(@"yourPath\books1.xml");
System.Xml.XmlNode myXmlNodeFirstBook = myXmlDocument.DocumentElement.FirstChild;
// Create a Book element and populate its attributes
System.Xml.XmlElement XmlElementMyBook = myXmlDocument.CreateElement("book");
XmlElementMyBook.SetAttribute("genre", "Funny");
XmlElementMyBook.SetAttribute("publicationdate", "21-03-2009");
XmlElementMyBook.SetAttribute("ISBN", "1-12345-11-0");
// Insert the new element into the XML tree under Catalog
myXmlDocument.DocumentElement.InsertBefore(XmlElementMyBook, myXmlNodeFirstBook);
// Create a new child of the book element
System.Xml.XmlElement myXmlElement2 = myXmlDocument.CreateElement("title");
myXmlElement2.InnerText = "Earthing";
// Insert the new element under the node we created
XmlElementMyBook.AppendChild(myXmlElement2);
// Create a new child of the book element
System.Xml.XmlElement myXmlElement3 = myXmlDocument.CreateElement("author");
XmlElementMyBook.AppendChild(myXmlElement3);
// Create new child of the author element
System.Xml.XmlElement myXmlElement4 = myXmlDocument.CreateElement("first-name");
myXmlElement4.InnerText = "Clint";
myXmlElement3.AppendChild(myXmlElement4);
// Create new child of the author element
System.Xml.XmlElement myXmlElement5 = myXmlDocument.CreateElement("last-name");
myXmlElement5.InnerText = "Ober";
myXmlElement3.AppendChild(myXmlElement5);
myXmlDocument.Save(@"yourPath\books1.xml");