XML与DataTable互转
今天项目组有人问我这个问题。简单实现了一下,有人需要,拿去。
Code
1class test
2 {
3
4 public static DataTable GetDataTable(string xmlStr)
5 {
6 XmlDocument doc = new XmlDocument();
7 doc.LoadXml(xmlStr);
8
9 XmlNodeList xlist = doc.SelectNodes("//DataTable/Rows");
10 DataTable Dt = new DataTable();
11 DataRow Dr;
12
13 for (int i = 0; i < xlist.Count; i++)
14 {
15 Dr = Dt.NewRow();
16 XmlElement xe = (XmlElement)xlist.Item(i);
17 for (int j = 0; j < xe.Attributes.Count; j++)
18 {
19 if (!Dt.Columns.Contains("@" + xe.Attributes[j].Name))
20 Dt.Columns.Add("@" + xe.Attributes[j].Name);
21 Dr["@" + xe.Attributes[j].Name] = xe.Attributes[j].Value;
22 }
23 for (int j = 0; j < xe.ChildNodes.Count; j++)
24 {
25 if (!Dt.Columns.Contains(xe.ChildNodes.Item(j).Name))
26 Dt.Columns.Add(xe.ChildNodes.Item(j).Name);
27 Dr[xe.ChildNodes.Item(j).Name] = xe.ChildNodes.Item(j).InnerText;
28 }
29 Dt.Rows.Add(Dr);
30 }
31 return Dt;
32 }
33
34
35 public static string GetXml(DataTable dt)
36 {
37 string strXml = @"<?xml version='1.0' encoding='UTF-8' ?><DataTable />";
38 XmlDocument doc = new XmlDocument();
39 doc.LoadXml(strXml);
40 XmlNode root = doc.SelectSingleNode("//DataTable");
41 // 创建子节点
42 for (int j = 0; j < dt.Rows.Count; j++)
43 {
44 XmlElement xe = doc.CreateElement("Rows");
45 XmlElement xeChild = null;
46 if (!Object.Equals(dt, null))
47 {
48 for (int i = 0; i < dt.Columns.Count; i++)
49 {
50 if (dt.Columns[i].ColumnName.StartsWith("@"))
51 {
52 string AttributeName = dt.Columns[i].ColumnName.Replace("@", "");
53 // 为该子节点设置属性
54 xe.SetAttribute(AttributeName, dt.Rows[j][i].ToString());
55 }
56 else
57 {
58 xeChild = doc.CreateElement(dt.Columns[i].ColumnName);
59
60 try
61 {
62 xeChild.InnerXml = dt.Rows[j][i].ToString();
63 }
64 catch
65 {
66 xeChild.InnerText = dt.Rows[j][i].ToString();
67 }
68 xe.AppendChild(xeChild);
69 }
70 }
71 }
72 // 保存子节点设置
73 root.AppendChild(xe);
74 }
75 return doc.InnerXml.ToString();
76 }
77 }
78//调用
79 DataTable dt = new DataTable("DataTable");
80 DataColumn dc1 = new DataColumn("merCode", Type.GetType("System.String"));
81 DataColumn dc2 = new DataColumn("crCode", Type.GetType("System.String"));
82 DataColumn dc3 = new DataColumn("startDate", Type.GetType("System.String"));
83 DataColumn dc4 = new DataColumn("endDate", Type.GetType("System.String"));
84 DataColumn dc5 = new DataColumn("hkID", Type.GetType("System.String"));
85 DataColumn dc6 = new DataColumn("printNo", Type.GetType("System.String"));
86 DataColumn dc7 = new DataColumn("LogInfo", Type.GetType("System.String"));
87 dt.Columns.Add(dc1);
88 dt.Columns.Add(dc2);
89 dt.Columns.Add(dc3);
90 dt.Columns.Add(dc4);
91 dt.Columns.Add(dc5);
92 dt.Columns.Add(dc6);
93 dt.Columns.Add(dc7);
94
95 DataRow dr = dt.NewRow();
96 dr["merCode"] = "1";
97 dr["crCode"] = "1";
98 dr["endDate"] = "1";
99 dt.Rows.Add(dr);
100
101 dr = dt.NewRow();
102 dr["merCode"] = "2";
103 dr["crCode"] = "2";
104 dr["endDate"] = "2";
105 dt.Rows.Add(dr);
106
107 Console.WriteLine(test.GetXml(dt));
108
109 Console.WriteLine("");
110
111
112 DataTable dts = test.GetDataTable(test.GetXml(dt));
113 for (int i = 0; i < dts.Rows.Count; i++)
114 {
115 string str = string.Empty;
116 for (int j = 0; j < dts.Columns.Count; j++)
117 {
118 str += dts.Rows[i][j].ToString();
119 }
120 Console.WriteLine(str);
121 }
122
123 Console.ReadLine();
1class test
2 {
3
4 public static DataTable GetDataTable(string xmlStr)
5 {
6 XmlDocument doc = new XmlDocument();
7 doc.LoadXml(xmlStr);
8
9 XmlNodeList xlist = doc.SelectNodes("//DataTable/Rows");
10 DataTable Dt = new DataTable();
11 DataRow Dr;
12
13 for (int i = 0; i < xlist.Count; i++)
14 {
15 Dr = Dt.NewRow();
16 XmlElement xe = (XmlElement)xlist.Item(i);
17 for (int j = 0; j < xe.Attributes.Count; j++)
18 {
19 if (!Dt.Columns.Contains("@" + xe.Attributes[j].Name))
20 Dt.Columns.Add("@" + xe.Attributes[j].Name);
21 Dr["@" + xe.Attributes[j].Name] = xe.Attributes[j].Value;
22 }
23 for (int j = 0; j < xe.ChildNodes.Count; j++)
24 {
25 if (!Dt.Columns.Contains(xe.ChildNodes.Item(j).Name))
26 Dt.Columns.Add(xe.ChildNodes.Item(j).Name);
27 Dr[xe.ChildNodes.Item(j).Name] = xe.ChildNodes.Item(j).InnerText;
28 }
29 Dt.Rows.Add(Dr);
30 }
31 return Dt;
32 }
33
34
35 public static string GetXml(DataTable dt)
36 {
37 string strXml = @"<?xml version='1.0' encoding='UTF-8' ?><DataTable />";
38 XmlDocument doc = new XmlDocument();
39 doc.LoadXml(strXml);
40 XmlNode root = doc.SelectSingleNode("//DataTable");
41 // 创建子节点
42 for (int j = 0; j < dt.Rows.Count; j++)
43 {
44 XmlElement xe = doc.CreateElement("Rows");
45 XmlElement xeChild = null;
46 if (!Object.Equals(dt, null))
47 {
48 for (int i = 0; i < dt.Columns.Count; i++)
49 {
50 if (dt.Columns[i].ColumnName.StartsWith("@"))
51 {
52 string AttributeName = dt.Columns[i].ColumnName.Replace("@", "");
53 // 为该子节点设置属性
54 xe.SetAttribute(AttributeName, dt.Rows[j][i].ToString());
55 }
56 else
57 {
58 xeChild = doc.CreateElement(dt.Columns[i].ColumnName);
59
60 try
61 {
62 xeChild.InnerXml = dt.Rows[j][i].ToString();
63 }
64 catch
65 {
66 xeChild.InnerText = dt.Rows[j][i].ToString();
67 }
68 xe.AppendChild(xeChild);
69 }
70 }
71 }
72 // 保存子节点设置
73 root.AppendChild(xe);
74 }
75 return doc.InnerXml.ToString();
76 }
77 }
78//调用
79 DataTable dt = new DataTable("DataTable");
80 DataColumn dc1 = new DataColumn("merCode", Type.GetType("System.String"));
81 DataColumn dc2 = new DataColumn("crCode", Type.GetType("System.String"));
82 DataColumn dc3 = new DataColumn("startDate", Type.GetType("System.String"));
83 DataColumn dc4 = new DataColumn("endDate", Type.GetType("System.String"));
84 DataColumn dc5 = new DataColumn("hkID", Type.GetType("System.String"));
85 DataColumn dc6 = new DataColumn("printNo", Type.GetType("System.String"));
86 DataColumn dc7 = new DataColumn("LogInfo", Type.GetType("System.String"));
87 dt.Columns.Add(dc1);
88 dt.Columns.Add(dc2);
89 dt.Columns.Add(dc3);
90 dt.Columns.Add(dc4);
91 dt.Columns.Add(dc5);
92 dt.Columns.Add(dc6);
93 dt.Columns.Add(dc7);
94
95 DataRow dr = dt.NewRow();
96 dr["merCode"] = "1";
97 dr["crCode"] = "1";
98 dr["endDate"] = "1";
99 dt.Rows.Add(dr);
100
101 dr = dt.NewRow();
102 dr["merCode"] = "2";
103 dr["crCode"] = "2";
104 dr["endDate"] = "2";
105 dt.Rows.Add(dr);
106
107 Console.WriteLine(test.GetXml(dt));
108
109 Console.WriteLine("");
110
111
112 DataTable dts = test.GetDataTable(test.GetXml(dt));
113 for (int i = 0; i < dts.Rows.Count; i++)
114 {
115 string str = string.Empty;
116 for (int j = 0; j < dts.Columns.Count; j++)
117 {
118 str += dts.Rows[i][j].ToString();
119 }
120 Console.WriteLine(str);
121 }
122
123 Console.ReadLine();