+ -
当前位置:首页 → 问答吧 → xml解析到Dataset报错

xml解析到Dataset报错

时间:2011-12-21

来源:互联网

我需要将一xml文档解析到dataset中,遇到DuplicateNameException 的报错,请问如何能将这个xml里的信息读出来。
xml文件
XML code

<PSISIG_CUSTOM_DESCRIPTOR>
    <BAT bouquet_id = "24677" >
        <DESCRIPTORS>
        </DESCRIPTORS>
        <LOOP original_network_id = "7340" ts_id = "1017" >
            <DESCRIPTORS>
                <DESCRIPTOR name = "private_data_specifier_descriptor" data = "5F0400" />
                <DESCRIPTOR name = "user_define_descriptor" data = "820406" />
            </DESCRIPTORS>
        </LOOP>
        <LOOP original_network_id = "7340" ts_id = "1014" >
            <DESCRIPTORS>
                <DESCRIPTOR name = "private_data_specifier_descriptor" data = "5F0400" />
                <DESCRIPTOR name = "user_define_descriptor" data = "8228057" />
                <DESCRIPTOR name = "user_define_descriptor" data = "850101" />                
            </DESCRIPTORS>
        </LOOP>
    </BAT>
    
    
     <NIT network_id="7340">
             <DESCRIPTORS>
           <DESCRIPTOR name = "user_define_descriptor" data = "4A1C00" />
             </DESCRIPTORS>
             <DESCRIPTORS>
             <DESCRIPTORS>
           <DESCRIPTOR name = "user_define_descriptor" data = "870A01" />
             </DESCRIPTORS>

      <LOOP ts_id = "254" original_network_id = "7340" service_id="2000">
      

          <DESCRIPTORS>
  </DESCRIPTORS>           
      </LOOP>
     </NIT>
</PSISIG_CUSTOM_DESCRIPTOR>





C#的代码是这样

C# code

DataSet ds = new DataSet();
ds.ReadXml(@"..\..\epg.XML")  //然后这里报错了



我的任务是将network_id 里的内容读出来并展现。

作者: such_beauty   发布时间: 2011-12-21

你的xml符合xml规则吗?
浏览器直接浏览你的xml看是否能解析

作者: net_lover   发布时间: 2011-12-21

//将xml对象内容字符串转换为DataSet
  public static DataSet ConvertXMLToDataSet(string xmlData)
  {
  StringReader stream = null;
  XmlTextReader reader = null;
  try
  {
  DataSet xmlDS = new DataSet();
  stream = new StringReader(xmlData);
  //从stream装载到XmlTextReader
  reader = new XmlTextReader(stream);
  xmlDS.ReadXml(reader);
  return xmlDS;
  }
  catch (System.Exception ex)
  {
  throw ex;
  }
  finally
  {
  if (reader != null)
  reader.Close();
  }
  }


  //将xml文件转换为DataSet
  public static DataSet ConvertXMLFileToDataSet(string xmlFile)
  {
  StringReader stream = null;
  XmlTextReader reader = null;
  try
  {
  XmlDocument xmld = new XmlDocument();
  xmld.Load(xmlFile);


  DataSet xmlDS = new DataSet();
  stream = new StringReader(xmld.InnerXml);
  //从stream装载到XmlTextReader
  reader = new XmlTextReader(stream);
  xmlDS.ReadXml(reader);
  //xmlDS.ReadXml(xmlFile);
  return xmlDS;
  }
  catch (System.Exception ex)
  {
  throw ex;
  }
  finally
  {
  if (reader != null)
  reader.Close();
  }
  }


  //将DataSet转换为xml对象字符串
  public static string ConvertDataSetToXML(DataSet xmlDS)
  {
  MemoryStream stream = null;
  XmlTextWriter writer = null;
  try
  {
  stream = new MemoryStream();
  //从stream装载到XmlTextReader
  writer = new XmlTextWriter(stream, Encoding.Unicode);


  //用WriteXml方法写入文件.
  xmlDS.WriteXml(writer);
  int count = (int)stream.Length;
  byte[] arr = new byte[count];
  stream.Seek(0, SeekOrigin.Begin);
  stream.Read(arr, 0, count);


  UnicodeEncoding utf = new UnicodeEncoding();
  return utf.GetString(arr).Trim();
  }
  catch (System.Exception ex)
  {
  throw ex;
  }
  finally
  {
  if (writer != null)
  writer.Close();
  }
  }


  //将DataSet转换为xml文件
  public static void ConvertDataSetToXMLFile(DataSet xmlDS, string xmlFile)
  {
  MemoryStream stream = null;
  XmlTextWriter writer = null;
  try
  {
  stream = new MemoryStream();
  //从stream装载到XmlTextReader
  writer = new XmlTextWriter(stream, Encoding.Unicode);


  //用WriteXml方法写入文件.
  xmlDS.WriteXml(writer);
  int count = (int)stream.Length;
  byte[] arr = new byte[count];
  stream.Seek(0, SeekOrigin.Begin);
  stream.Read(arr, 0, count);


  //返回Unicode编码的文本
  UnicodeEncoding utf = new UnicodeEncoding();
  StreamWriter sw = new StreamWriter(xmlFile);
  sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
  sw.WriteLine(utf.GetString(arr).Trim());
  sw.Close();
  }
  catch (System.Exception ex)
  {
  throw ex;
  }
  finally
  {
  if (writer != null)
  writer.Close();
  }
  }

作者: hefeng_aspnet   发布时间: 2011-12-21

引用 1 楼 net_lover 的回复:

你的xml符合xml规则吗?
浏览器直接浏览你的xml看是否能解析


浏览器能解析 单独解析NIT表和BAT表都能成功,但是放在一起就失败了。

作者: such_beauty   发布时间: 2011-12-21