code_junkie

XML Parsing

Nov 12th, 2011
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. <bookstore>
  2. <books>
  3. <author>
  4. </author>
  5. </books>
  6. </bookstore>
  7.  
  8. <bookstore>
  9. <newnode>
  10. <books>
  11. <author>
  12. </author>
  13. </books>
  14. </newnode>
  15. </bookstore>
  16.  
  17. XmlDocument doc = new XmlDocument();
  18. doc.Load("BookStore.xml");
  19. XmlElement newNode = doc.CreateElement("newnode");
  20. doc.DocumentElement.AppendChild(newNode);
  21. newNode.AppendChild(doc.SelectSingleNode("/bookstore/books"));
  22. doc.Save("BookStore.xml");
  23.  
  24. XmlDocument xd = new XmlDocument();
  25. xd.Load("oldxmlfile.xml");
  26. XmlNode oldNode = xd["nameOfRootNode"];
  27. xd.RemoveAll();
  28. XmlNode newParent = xd.CreateNode("nodename");
  29. newParent.AppendChild(oldNode);
  30. xd.AppendChild(newParent);
  31. xd.Save("newXmlFile.xml");
  32.  
  33. XmlDocument doc = new XmlDocument();
  34. // load the current xml
  35. doc.LoadXml(xml);
  36. // create a new "newnode" node and add it into the tree
  37. XmlElement newnode = (XmlElement) doc.DocumentElement.AppendChild(doc.CreateElement("newnode"));
  38. // locate the original "books" node and move it
  39. newnode.AppendChild(doc.SelectSingleNode("/bookstore/books"));
  40. // show the result
  41. Console.WriteLine(doc.OuterXml);
Add Comment
Please, Sign In to add comment