问题描述
我需要将一xml文档解析到dataset中,遇到DuplicateNameException的报错,请问如何能将这个xml里的信息读出来。xml文件<PSISIG_CUSTOM_DESCRIPTOR><BATbouquet_id="24677"><DESCRIPTORS></DESCRIPTORS><LOOPoriginal_network_id="7340"ts_id="1017"><DESCRIPTORS><DESCRIPTORname="private_data_specifier_descriptor"data="5F0400"/><DESCRIPTORname="user_define_descriptor"data="820406"/></DESCRIPTORS></LOOP><LOOPoriginal_network_id="7340"ts_id="1014"><DESCRIPTORS><DESCRIPTORname="private_data_specifier_descriptor"data="5F0400"/><DESCRIPTORname="user_define_descriptor"data="8228057"/><DESCRIPTORname="user_define_descriptor"data="850101"/></DESCRIPTORS></LOOP></BAT><NITnetwork_id="7340"><DESCRIPTORS><DESCRIPTORname="user_define_descriptor"data="4A1C00"/></DESCRIPTORS><DESCRIPTORS><DESCRIPTORS><DESCRIPTORname="user_define_descriptor"data="870A01"/></DESCRIPTORS><LOOPts_id="254"original_network_id="7340"service_id="2000"><DESCRIPTORS></DESCRIPTORS></LOOP></NIT></PSISIG_CUSTOM_DESCRIPTOR>
C#的代码是这样DataSetds=newDataSet();ds.ReadXml(@"....epg.XML")//然后这里报错了
我的任务是将network_id里的内容读出来并展现。
解决方案
本帖最后由 such_beauty 于 2011-12-21 14:52:54 编辑
解决方案二:
你的xml符合xml规则吗?浏览器直接浏览你的xml看是否能解析
解决方案三:
//将xml对象内容字符串转换为DataSetpublicstaticDataSetConvertXMLToDataSet(stringxmlData){StringReaderstream=null;XmlTextReaderreader=null;try{DataSetxmlDS=newDataSet();stream=newStringReader(xmlData);//从stream装载到XmlTextReaderreader=newXmlTextReader(stream);xmlDS.ReadXml(reader);returnxmlDS;}catch(System.Exceptionex){throwex;}finally{if(reader!=null)reader.Close();}}//将xml文件转换为DataSetpublicstaticDataSetConvertXMLFileToDataSet(stringxmlFile){StringReaderstream=null;XmlTextReaderreader=null;try{XmlDocumentxmld=newXmlDocument();xmld.Load(xmlFile);DataSetxmlDS=newDataSet();stream=newStringReader(xmld.InnerXml);//从stream装载到XmlTextReaderreader=newXmlTextReader(stream);xmlDS.ReadXml(reader);//xmlDS.ReadXml(xmlFile);returnxmlDS;}catch(System.Exceptionex){throwex;}finally{if(reader!=null)reader.Close();}}//将DataSet转换为xml对象字符串publicstaticstringConvertDataSetToXML(DataSetxmlDS){MemoryStreamstream=null;XmlTextWriterwriter=null;try{stream=newMemoryStream();//从stream装载到XmlTextReaderwriter=newXmlTextWriter(stream,Encoding.Unicode);//用WriteXml方法写入文件.xmlDS.WriteXml(writer);intcount=(int)stream.Length;byte[]arr=newbyte[count];stream.Seek(0,SeekOrigin.Begin);stream.Read(arr,0,count);UnicodeEncodingutf=newUnicodeEncoding();returnutf.GetString(arr).Trim();}catch(System.Exceptionex){throwex;}finally{if(writer!=null)writer.Close();}}//将DataSet转换为xml文件publicstaticvoidConvertDataSetToXMLFile(DataSetxmlDS,stringxmlFile){MemoryStreamstream=null;XmlTextWriterwriter=null;try{stream=newMemoryStream();//从stream装载到XmlTextReaderwriter=newXmlTextWriter(stream,Encoding.Unicode);//用WriteXml方法写入文件.xmlDS.WriteXml(writer);intcount=(int)stream.Length;byte[]arr=newbyte[count];stream.Seek(0,SeekOrigin.Begin);stream.Read(arr,0,count);//返回Unicode编码的文本UnicodeEncodingutf=newUnicodeEncoding();StreamWritersw=newStreamWriter(xmlFile);sw.WriteLine("<?xmlversion="1.0"encoding="utf-8"?>");sw.WriteLine(utf.GetString(arr).Trim());sw.Close();}catch(System.Exceptionex){throwex;}finally{if(writer!=null)writer.Close();}}
解决方案四:
引用1楼net_lover的回复:
你的xml符合xml规则吗?浏览器直接浏览你的xml看是否能解析
浏览器能解析单独解析NIT表和BAT表都能成功,但是放在一起就失败了。
解决方案五:
lz,你的xml格式明显有问题:<NITnetwork_id="7340"><DESCRIPTORS><DESCRIPTORname="user_define_descriptor"data="4A1C00"/></DESCRIPTORS><DESCRIPTORS><DESCRIPTORS><DESCRIPTORname="user_define_descriptor"data="870A01"/></DESCRIPTORS><LOOPts_id="254"original_network_id="7340"service_id="2000"><DESCRIPTORS></DESCRIPTORS></LOOP></NIT>你自己仔细看看,明显节点不对应,浏览器根本打不开!
解决方案六:
XML有问题,第26行的节点<DESCRIPTORS>有问题,无法匹配。
解决方案七:
真对不起我的xml文件贴错了,正确的XML文件是这样的<PSISIG_CUSTOM_DESCRIPTOR><BATbouquet_id="24677"><DESCRIPTORS></DESCRIPTORS><LOOPoriginal_network_id="7340"ts_id="1017"><DESCRIPTORS><DESCRIPTORname="private_data_specifier_descriptor"data="5F0400000000"/><DESCRIPTORname="user_define_descriptor"data="820406A50080"/></DESCRIPTORS></LOOP><LOOPoriginal_network_id="7340"ts_id="101"><DESCRIPTORS><DESCRIPTORname="private_data_specifier_descriptor"data="5F0400000000"/><DESCRIPTORname="user_define_descriptor"data="821C0065000100660002006700030068000400690005006A0006006B0007"/></DESCRIPTORS></LOOP><LOOPoriginal_network_id="7340"ts_id="102"><DESCRIPTORS><DESCRIPTORname="private_data_specifier_descriptor"data="5F0400000000"/><DESCRIPTORname="user_define_descriptor"data="821800C9000800CA000900CB000A00CC000B00CD000C00CE000D"/></DESCRIPTORS></LOOP></BAT><NITnetwork_id="7340"><DESCRIPTORS><DESCRIPTORname="user_define_descriptor"data="4A1C00FE00020001A00100000000000000090000000007D1D16200FFFF80"/></DESCRIPTORS><DESCRIPTORS><DESCRIPTORname="user_define_descriptor"data="4A1C00FE00020002A00200000000000000090000000007D1D16200FFFF80"/></DESCRIPTORS><DESCRIPTORS><DESCRIPTORname="user_define_descriptor"data="4A1C00FE00020003A00300000000000000090000000007D1D16200FFFF80"/></DESCRIPTORS><LOOPts_id="254"original_network_id="7340"service_id="2000"><DESCRIPTORS></DESCRIPTORS></LOOP></NIT></PSISIG_CUSTOM_DESCRIPTOR>
报错是这样“无法添加名为“DESCRIPTORS”的列:此数据表中已存在同名的嵌套表。”请高人帮我查下原因。