Posted By

krisdb on 01/14/09


Tagged

c-sharp


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

umang_nine
ddevening
khouser


XML document create


 / Published in: C#
 

  1. string fileName = MapPath("file.xml");
  2.  
  3. XmlDocument xmlDoc = new XmlDocument();
  4.  
  5. XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
  6. xmlDoc.AppendChild(dec);
  7.  
  8. //root node
  9. XmlElement root = xmlDoc.CreateElement("root");
  10. xmlDoc.AppendChild(root);
  11.  
  12. //parent node
  13. XmlElement albumNode = xmlDoc.CreateElement("parent");
  14. albumNode.SetAttribute("title", "");
  15. albumNode.SetAttribute("description", "");
  16. root.AppendChild(albumNode);
  17.  
  18. //process child nodes
  19. XmlElement newNode;
  20. foreach (Something item in SomethingList)
  21. {
  22. newNode = xmlDoc.CreateElement("child");
  23. newNode.SetAttribute("attr", item.Text);
  24. newNode.InnerText = "text";
  25. albumNode.AppendChild(newNode);
  26. }
  27.  
  28. //save the file
  29. xmlDoc.Save(fileName);

Report this snippet  

You need to login to post a comment.