C# 导出XML文件

数据库表内信息   表名为CP 

 

 1 public void CreateXmlFile()
 2         {
 3             using (ws1DataContext con = new ws1DataContext())
 4             {
 5 
 6                 XmlDocument xmlDoc = new XmlDocument();
 7                 //创建类型声明节点    
 8                 XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");
 9                 xmlDoc.AppendChild(node);
10 
11 
12                 List<CP> clist = new List<CP>();
13                 clist = con.CP.ToList();
14                 int ii = clist.Count;
15                 //创建根节点    
16                 XmlNode root = xmlDoc.CreateElement("User");
17 
18                 xmlDoc.AppendChild(root);
19                 for (int i = 0; i < ii; i++)
20                 {
21                     CreateNode(xmlDoc, root, "Ids", clist[i].Ids.ToString());
22                     CreateNode(xmlDoc, root, "Id", clist[i].ID.ToString());
23                     CreateNode(xmlDoc, root, "PC", clist[i].PJ.ToString());
24                 }
25                 try
26                 {
27                     xmlDoc.Save("c://data2.xml");
28                 }
29                 catch (Exception e)
30                 {
31                     //显示错误信息    
32                     Console.WriteLine(e.Message);
33                 }
34                 //Console.ReadLine();    
35 
36             }
37         }
38         /// <summary>      
39         /// 创建节点      
40         /// </summary>      
41         /// <param name="xmldoc"></param>  xml文档    
42         /// <param name="parentnode"></param>父节点      
43         /// <param name="name"></param>  节点名    
44         /// <param name="value"></param>  节点值    
45         ///     
46         public void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string name, string value)
47         {
48             XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null);
49             node.InnerText = value;
50             parentNode.AppendChild(node);
51         }


导出的XML信息

 

无敌!

 

posted @ 2017-12-07 14:45  不会撩妹的白芒果  阅读(3919)  评论(0编辑  收藏  举报