Append new node to XML file
-
I have written an XML file using this code
private void AddBook(string ISBN, string title, string author)
{//
// Initialize booksElement if you are adding your first book.
//
if (booksElement == null)
{booksElement = xmlDoc.CreateElement("Books");
}
//
// Same as tutorial; Adding and populating the elements
//
XmlElement bookElement = xmlDoc.CreateElement("Book");XmlAttribute bookAttribute = xmlDoc.CreateAttribute("ISBN");
bookElement.SetAttributeNode(bookAttribute);
bookAttribute.Value = ISBN;XmlElement titleElement = xmlDoc.CreateElement("title");
titleElement.InnerText = title;
bookElement.AppendChild(titleElement);XmlElement authorElement = xmlDoc.CreateElement("author");
authorElement.InnerText = author;
bookElement.AppendChild(authorElement);booksElement.AppendChild(bookElement);
xmlDoc.AppendChild(booksElement);
xmlDoc.Save(path);}
it works fine for a new book, but I cannot OPEN an existing xml file and add a book (node) to it. How do I append a node??
_____________________________________________________ Yea! I could be wrong...
-
I have written an XML file using this code
private void AddBook(string ISBN, string title, string author)
{//
// Initialize booksElement if you are adding your first book.
//
if (booksElement == null)
{booksElement = xmlDoc.CreateElement("Books");
}
//
// Same as tutorial; Adding and populating the elements
//
XmlElement bookElement = xmlDoc.CreateElement("Book");XmlAttribute bookAttribute = xmlDoc.CreateAttribute("ISBN");
bookElement.SetAttributeNode(bookAttribute);
bookAttribute.Value = ISBN;XmlElement titleElement = xmlDoc.CreateElement("title");
titleElement.InnerText = title;
bookElement.AppendChild(titleElement);XmlElement authorElement = xmlDoc.CreateElement("author");
authorElement.InnerText = author;
bookElement.AppendChild(authorElement);booksElement.AppendChild(bookElement);
xmlDoc.AppendChild(booksElement);
xmlDoc.Save(path);}
it works fine for a new book, but I cannot OPEN an existing xml file and add a book (node) to it. How do I append a node??
_____________________________________________________ Yea! I could be wrong...
Hi! You didn't show how you create the
xmlDoc
object, butXmlDocument
has aLoad()
method you can use to open an existing xml file. If you do open an existing file, you don't have to create the root entry (I guess it'sbooksElement
, but can usexmlDoc.SelectSingleNode("/Books")
to retrieve a reference to the existing node.Regards, mav -- Black holes are the places where God divided by 0...
-
Hi! You didn't show how you create the
xmlDoc
object, butXmlDocument
has aLoad()
method you can use to open an existing xml file. If you do open an existing file, you don't have to create the root entry (I guess it'sbooksElement
, but can usexmlDoc.SelectSingleNode("/Books")
to retrieve a reference to the existing node.Regards, mav -- Black holes are the places where God divided by 0...
I'm sorry I posted only the function of creating one node... I grabbed code from www.kirupa.com , all of it is here..
private XmlDocument xmlDoc = new XmlDocument();
private XmlElement booksElement;private void AddBook(string ISBN, string title, string author)
{//
// Initialize booksElement if you are adding your first book.
//
if (booksElement == null)
{booksElement = xmlDoc.CreateElement("Books");
}
XmlElement bookElement = xmlDoc.CreateElement("Book");
XmlAttribute bookAttribute = xmlDoc.CreateAttribute("ISBN");
bookElement.SetAttributeNode(bookAttribute);
bookAttribute.Value = ISBN;XmlElement titleElement = xmlDoc.CreateElement("title");
titleElement.InnerText = title;
bookElement.AppendChild(titleElement);XmlElement authorElement = xmlDoc.CreateElement("author");
authorElement.InnerText = author;
bookElement.AppendChild(authorElement);booksElement.AppendChild(bookElement);
}
private void WriteToDisk(string path)
{// Add the booksElement to our root element and write to disk
xmlDoc.AppendChild(booksElement);
xmlDoc.Save(path);}
static void Main(string[] args)
{Program bookList = new Program();
bookList.AddBook("0553212419", "Sherlock Holmes: Complete Novels and Stories, Vol 1", "Sir Arthur Conan Doyle");
bookList.AddBook("0743273567", "The Great Gatsby", "F. Scott Fitzgerald");
bookList.AddBook("0684826976", "Undaunted Courage", "Stephen E. Ambrose");
bookList.AddBook("0743203178", "Nothing Like It In the World", "Stephen E. Ambrose");bookList.WriteToDisk(@"C:\books.xml");
}
So you see this creates a file OK.... But If I cannot ue this function to Append to the saved books.xml file. meanwhile, I try the SelectSingleNode, and see if it works.
_____________________________________________________ Yea! I could be wrong...