C#对XML文件的读操作
比较弱智的方式....有好的请留言
Code
1XmlDocument xmldoc = new XmlDocument();
2 xmldoc.Load("xml/WapHomePage.xml");
3 XmlNodeList nodeList = xmldoc.GetElementsByTagName("WapHomePage");
4 foreach (XmlNode node in nodeList)
5 {
6 if (node.SelectSingleNode("WapInfo") != null)
7 Response.Write(node.SelectSingleNode("WapInfo").InnerText + "</br>");
8 if (node.SelectSingleNode("WapNewPublishVersion") != null)
9 Response.Write(node.SelectSingleNode("WapNewPublishVersion").InnerText + "</br>");
10 if (node.SelectSingleNode("WapNewPublishDate") != null)
11 Response.Write(node.SelectSingleNode("WapNewPublishDate").InnerText + "</br>");
12 if (node.SelectSingleNode("WapHistoryCount") != null)
13 Response.Write(node.SelectSingleNode("WapHistoryCount").InnerText + "</br>");
14 if (node.SelectSingleNode("WapLinkCount") != null)
15 Response.Write(node.SelectSingleNode("WapLinkCount").InnerText + "</br>");
16 if (node.SelectSingleNode("WapHistoryInfo") != null)
17 {
18 XmlNodeList list = node.SelectSingleNode("WapHistoryInfo").ChildNodes;
19 foreach (XmlNode n in list)
20 {
21 XmlElement xe = (XmlElement)n;
22 Response.Write(xe.GetAttribute("key") + "</br>");
23 Response.Write(xe.GetAttribute("value") + "</br>");
24 }
25 }
26
27 if (node.SelectSingleNode("WapLinkInfo") != null)
28 {
29 XmlNodeList list = node.SelectSingleNode("WapLinkInfo").ChildNodes;
30 foreach (XmlNode n in list)
31 {
32 XmlElement xe = (XmlElement)n;
33 Response.Write(xe.GetAttribute("key") + "</br>");
34 Response.Write(xe.GetAttribute("value") + "</br>");
35 }
36 }
37
38
1XmlDocument xmldoc = new XmlDocument();
2 xmldoc.Load("xml/WapHomePage.xml");
3 XmlNodeList nodeList = xmldoc.GetElementsByTagName("WapHomePage");
4 foreach (XmlNode node in nodeList)
5 {
6 if (node.SelectSingleNode("WapInfo") != null)
7 Response.Write(node.SelectSingleNode("WapInfo").InnerText + "</br>");
8 if (node.SelectSingleNode("WapNewPublishVersion") != null)
9 Response.Write(node.SelectSingleNode("WapNewPublishVersion").InnerText + "</br>");
10 if (node.SelectSingleNode("WapNewPublishDate") != null)
11 Response.Write(node.SelectSingleNode("WapNewPublishDate").InnerText + "</br>");
12 if (node.SelectSingleNode("WapHistoryCount") != null)
13 Response.Write(node.SelectSingleNode("WapHistoryCount").InnerText + "</br>");
14 if (node.SelectSingleNode("WapLinkCount") != null)
15 Response.Write(node.SelectSingleNode("WapLinkCount").InnerText + "</br>");
16 if (node.SelectSingleNode("WapHistoryInfo") != null)
17 {
18 XmlNodeList list = node.SelectSingleNode("WapHistoryInfo").ChildNodes;
19 foreach (XmlNode n in list)
20 {
21 XmlElement xe = (XmlElement)n;
22 Response.Write(xe.GetAttribute("key") + "</br>");
23 Response.Write(xe.GetAttribute("value") + "</br>");
24 }
25 }
26
27 if (node.SelectSingleNode("WapLinkInfo") != null)
28 {
29 XmlNodeList list = node.SelectSingleNode("WapLinkInfo").ChildNodes;
30 foreach (XmlNode n in list)
31 {
32 XmlElement xe = (XmlElement)n;
33 Response.Write(xe.GetAttribute("key") + "</br>");
34 Response.Write(xe.GetAttribute("value") + "</br>");
35 }
36 }
37
38
自己写的另一种
Code
1//获取请求SQL内容
2 StreamReader reader = new StreamReader(Request.InputStream);
3 string xml = reader.ReadToEnd();
4 //string xml = "<?xml version=\"1.0\"?><misc_command version=\"1.5\"><command_name>provision</command_name><command_data_block><action_id>1</action_id><service_id>01112233</service_id><mid>00148888888888</mid><mobile_id>13999998888</mobile_id><access_mode>2</access_mode><access_mode>2</access_mode><access_mode>2</access_mode></command_data_block></misc_command>";
5 reader.Close();
6 XmlDocument xmlDoc = new XmlDocument();
7 xmlDoc.InnerXml = xml;
8 int result_id = 0;
9 string result_string = "successful";
10 string actionID = "";
11 string serviceID = "";
12 string mid = "";
13 string mobileID = "";
14 XmlNodeList nodeList = xmlDoc.SelectSingleNode("misc_command").ChildNodes;
15 foreach (XmlNode xn in nodeList)
16 {
17 XmlElement xe = (XmlElement)xn;
18 if (xe.Name == "command_data_block")
19 {
20 XmlNodeList nls = xe.ChildNodes;
21 foreach (XmlNode xn1 in nls)
22 {
23 XmlElement xe2 = (XmlElement)xn1;
24 if (xe2.Name == "action_id")
25 {
26 actionID = xe2.InnerText;
27 if (Convert.ToInt32(xe2.InnerText) < 1 || Convert.ToInt32(xe2.InnerText) > 4)
28 {
29 result_id = -1;
30 result_string = "failed";
31 break;
32 }
33 }
34 if (xe2.Name == "service_id")
35 {
36 serviceID = xe2.InnerText;
37 if (xe2.InnerText.Length < 8)
38 {
39 result_id = -2;
40 result_string = "failed";
41 break;
42 }
43 }
44 if (xe2.Name == "mid")
45 {
46 mid = xe2.InnerText;
47 if (xe2.InnerText.Length < 14)
48 {
49 result_id = -3;
50 result_string = "failed";
51 break;
52 }
53 }
54 if (xe2.Name == "mobile_id")
55 {
56 mobileID = xe2.InnerText;
57 if (xe2.InnerText.Length < 9)
58 {
59 result_id = -3;
60 result_string = "failed";
61 break;
62 }
63 }
64 if (xe2.Name == "access_mode")
65 {
66 if (Convert.ToInt32(xe2.InnerText) < 1 || Convert.ToInt32(xe2.InnerText) > 3)
67 {
68 result_id = -3;
69 result_string = "failed";
70 break;
71 }
72 }
73 }
74 break;
75 }
76
1//获取请求SQL内容
2 StreamReader reader = new StreamReader(Request.InputStream);
3 string xml = reader.ReadToEnd();
4 //string xml = "<?xml version=\"1.0\"?><misc_command version=\"1.5\"><command_name>provision</command_name><command_data_block><action_id>1</action_id><service_id>01112233</service_id><mid>00148888888888</mid><mobile_id>13999998888</mobile_id><access_mode>2</access_mode><access_mode>2</access_mode><access_mode>2</access_mode></command_data_block></misc_command>";
5 reader.Close();
6 XmlDocument xmlDoc = new XmlDocument();
7 xmlDoc.InnerXml = xml;
8 int result_id = 0;
9 string result_string = "successful";
10 string actionID = "";
11 string serviceID = "";
12 string mid = "";
13 string mobileID = "";
14 XmlNodeList nodeList = xmlDoc.SelectSingleNode("misc_command").ChildNodes;
15 foreach (XmlNode xn in nodeList)
16 {
17 XmlElement xe = (XmlElement)xn;
18 if (xe.Name == "command_data_block")
19 {
20 XmlNodeList nls = xe.ChildNodes;
21 foreach (XmlNode xn1 in nls)
22 {
23 XmlElement xe2 = (XmlElement)xn1;
24 if (xe2.Name == "action_id")
25 {
26 actionID = xe2.InnerText;
27 if (Convert.ToInt32(xe2.InnerText) < 1 || Convert.ToInt32(xe2.InnerText) > 4)
28 {
29 result_id = -1;
30 result_string = "failed";
31 break;
32 }
33 }
34 if (xe2.Name == "service_id")
35 {
36 serviceID = xe2.InnerText;
37 if (xe2.InnerText.Length < 8)
38 {
39 result_id = -2;
40 result_string = "failed";
41 break;
42 }
43 }
44 if (xe2.Name == "mid")
45 {
46 mid = xe2.InnerText;
47 if (xe2.InnerText.Length < 14)
48 {
49 result_id = -3;
50 result_string = "failed";
51 break;
52 }
53 }
54 if (xe2.Name == "mobile_id")
55 {
56 mobileID = xe2.InnerText;
57 if (xe2.InnerText.Length < 9)
58 {
59 result_id = -3;
60 result_string = "failed";
61 break;
62 }
63 }
64 if (xe2.Name == "access_mode")
65 {
66 if (Convert.ToInt32(xe2.InnerText) < 1 || Convert.ToInt32(xe2.InnerText) > 3)
67 {
68 result_id = -3;
69 result_string = "failed";
70 break;
71 }
72 }
73 }
74 break;
75 }
76