daixinet

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
解决方案:
 
 1、把头信息<?xml version='1.0' encoding='UTF-8'?>,但是内容会丢失部分;
2、用XmlDocument解析就OK。
 
正确代码:
            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。
代码:
 
 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();
        }

  

posted on 2014-05-24 16:01  daixinet.com  阅读(4109)  评论(0编辑  收藏  举报