首页 > 代码库 > XML内容作为String字符串读取报错
XML内容作为String字符串读取报错
解决方案:
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(); } |
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。