Home
Manage Your Code
Snippet: Create XML Document from a list (C#)
Title: Create XML Document from a list Language: C#
Description: Creates an XML Document from a List Views: 255
Author: Tony Meacham Date Added: 6/22/2009
Copy Code  
1	private void MakeXMLDoc()
2        {
3            string fileName = MapPath("file.xml");
4
5            XmlDocument xmlDoc = new XmlDocument();
6
7            XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
8            xmlDoc.AppendChild(dec);
9
10            //root node
11            XmlElement root = xmlDoc.CreateElement("root");
12            xmlDoc.AppendChild(root);
13
14            //parent node
15            XmlElement albumNode = xmlDoc.CreateElement("parent");
16            albumNode.SetAttribute("title", "");
17            albumNode.SetAttribute("description", "");
18            root.AppendChild(albumNode);
19
20            //process child nodes
21            XmlElement newNode;
22            foreach (Something item in SomethingList)
23            {
24                newNode = xmlDoc.CreateElement("child");
25                newNode.SetAttribute("attr", item.Text);
26                newNode.InnerText = "text";
27                albumNode.AppendChild(newNode);
28            }
29
30            //save the file
31            xmlDoc.Save(fileName);
32        
33        
34        }