1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 |
string
xmlStr = "<?xml version=‘1.0‘ encoding=‘UTF-8‘?><ROOT><MAIN_BILL_ID>13905837718</MAIN_BILL_ID><PROD_ID>600000044996</PROD_ID></ROOT>" ; xmlStr = "<?xml version=‘1.0‘ encoding=‘UTF-8‘?><ROOT><MAIN_BILL_ID></MAIN_BILL_ID><DEL_BILLID_LIST><DEL_BILLID_INFO><BILL_ID>13905837765</BILL_ID><SHORT_NUM>661</SHORT_NUM></DEL_BILLID_INFO><DEL_BILLID_INFO><BILL_ID>13456381111</BILL_ID><SHORT_NUM>665</SHORT_NUM></DEL_BILLID_INFO><DEL_BILLID_INFO><BILL_ID>13819039202</BILL_ID><SHORT_NUM>662</SHORT_NUM></DEL_BILLID_INFO><DEL_BILLID_INFO><BILL_ID>15857301025</BILL_ID><SHORT_NUM>667</SHORT_NUM></DEL_BILLID_INFO><DEL_BILLID_INFO><BILL_ID>13857342868</BILL_ID><SHORT_NUM>666</SHORT_NUM></DEL_BILLID_INFO><DEL_BILLID_INFO><BILL_ID>13957366314</BILL_ID><SHORT_NUM>668</SHORT_NUM></DEL_BILLID_INFO><DEL_BILLID_INFO><BILL_ID>18267350517</BILL_ID><SHORT_NUM>669</SHORT_NUM></DEL_BILLID_INFO></DEL_BILLID_LIST></ROOT>" ; StringBuilder rtn = new
StringBuilder(); XmlDocument xmlDoc = new
XmlDocument(); xmlDoc.LoadXml(xmlStr); XmlNodeList xmlNodeList = xmlDoc.GetElementsByTagName( "ROOT" ); foreach
(XmlNode xmlNode in
xmlNodeList) { //家长节点 XmlNodeList childXmlNodes1 = xmlNode.ChildNodes; foreach
(XmlNode childXmlNode1 in
childXmlNodes1) { if
(childXmlNode1.Name == "MAIN_BILL_ID" ) { string
tel = childXmlNode1.InnerText; if
(rtn.Length > 0) { rtn.Append( "," ); } rtn.Append(tel); } //成员列表 XmlNodeList childXmlNodes2 = childXmlNode1.ChildNodes; foreach
(XmlNode childXmlNode2 in
childXmlNodes2) { //成员详情 XmlNodeList childXmlNodes3 = childXmlNode2.ChildNodes; foreach
(XmlNode childXmlNode3 in
childXmlNodes3) { if
(childXmlNode3.Name == "BILL_ID"
|| childXmlNode3.Name == "SHORT_NUM" ) { string
tel = childXmlNode3.InnerText; if
(rtn.Length > 0) { rtn.Append( "," ); } rtn.Append(tel); } } } } } Console.WriteLine(rtn.ToString()); Console.WriteLine( "" ); |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 |
static
void Main( string [] args) { string
xmlStr = "<?xml version=‘1.0‘encoding=‘UTF-8‘?><ROOT><MAIN_BILL_ID>13905837718</MAIN_BILL_ID><PROD_ID>600000044996</PROD_ID></ROOT>" ; StringBuilder rtn = new
StringBuilder(); //xmlStr = xmlStr.Replace("<?xml version=‘1.0‘encoding=‘UTF-8‘?>", ""); if
(! string .IsNullOrEmpty(xmlStr)) { XmlReader xmlReader = null ; try { MemoryStream memStream = new
MemoryStream(); XmlWriter writer = XmlWriter.Create(memStream); writer.WriteRaw(xmlStr); writer.Flush(); writer.Close(); memStream.Position = 0; xmlReader = XmlReader.Create(memStream); //xmlReader = XmlReader.Create(@"E:\01.xml"); while
( xmlReader.Read()) { if
(xmlReader.NodeType == XmlNodeType.Element) { switch
(xmlReader.Name) { case
"MAIN_BILL_ID" : string
tel = xmlReader.ReadInnerXml().Trim(); if
( string .IsNullOrEmpty(tel) == false ) { rtn.Append(tel); rtn.Append( ";" ); } break ; case
"BILL_ID" : tel = xmlReader.ReadInnerXml().Trim(); if
( string .IsNullOrEmpty(tel) == false ) { rtn.Append(tel); rtn.Append( "," ); } break ; case
"SHORT_NUM" : tel = xmlReader.ReadInnerXml().Trim(); if
( string .IsNullOrEmpty(tel) == false ) { rtn.Append(tel); rtn.Append( ";" ); } break ; } } } } catch
(Exception ex) { rtn.Append(ex.Message); } finally { if
(xmlReader != null ) { xmlReader.Close(); } } } Console.WriteLine(rtn.ToString()); Console.WriteLine( "" ); Console.ReadKey(); } |
XML内容作为String字符串读取报错,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/sshoub/p/3749963.html