首页 > 其他 > 详细

XML内容作为String字符串读取报错

时间:2014-05-26 00:12:34      阅读:508      评论:0      收藏:0      [点我收藏+]
解决方案:
 
 1、把头信息<?xml version=‘1.0‘ encoding=‘UTF-8‘?>,但是内容会丢失部分;
2、用XmlDocument解析就OK。
 
正确代码:
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("");

  

 
 
 
错误信息:
意外的 XML 声明。XML 声明必须是文档中的第一个节点并且在它之前不允许出现空白字符。 第 1 行,位置 41。
代码:
 
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

XML内容作为String字符串读取报错

原文:http://www.cnblogs.com/sshoub/p/3749963.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!