当前位置:编程学习 > C#/ASP.NET >>

xml解析到Dataset报错

--------------------编程问答-------------------- 你的xml符合xml规则吗?
浏览器直接浏览你的xml看是否能解析 --------------------编程问答-------------------- //将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();
            }
        } --------------------编程问答--------------------
引用 1 楼 net_lover 的回复:
你的xml符合xml规则吗?
浏览器直接浏览你的xml看是否能解析


浏览器能解析 单独解析NIT表和BAT表都能成功,但是放在一起就失败了。 --------------------编程问答-------------------- lz,你的xml格式明显有问题:
     <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>


你自己仔细看看,明显节点不对应,浏览器根本打不开! --------------------编程问答-------------------- XML有问题,第26行的节点<DESCRIPTORS>有问题,无法匹配。 --------------------编程问答-------------------- 真对不起 我的xml文件贴错了,正确的XML文件是这样的 
<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 = "5F0400000000" />
<DESCRIPTOR name = "user_define_descriptor" data = "820406A50080" />
</DESCRIPTORS>
</LOOP>
<LOOP original_network_id = "7340" ts_id = "101" >
<DESCRIPTORS>
<DESCRIPTOR name = "private_data_specifier_descriptor" data = "5F0400000000" />
<DESCRIPTOR name = "user_define_descriptor" data = "821C0065000100660002006700030068000400690005006A0006006B0007" />
</DESCRIPTORS>
</LOOP>
<LOOP original_network_id = "7340" ts_id = "102" >
<DESCRIPTORS>
<DESCRIPTOR name = "private_data_specifier_descriptor" data = "5F0400000000" />
<DESCRIPTOR name = "user_define_descriptor" 
                                            data ="821800C9000800CA000900CB000A00CC000B00CD000C00CE000D" />
</DESCRIPTORS>
</LOOP>

</BAT>
     <NIT network_id="7340">
             <DESCRIPTORS>
           <DESCRIPTOR name = "user_define_descriptor" data = "4A1C00FE00020001A00100000000000000090000000007D1D16200FFFF80" />
             </DESCRIPTORS>
             <DESCRIPTORS>
           <DESCRIPTOR name = "user_define_descriptor" data = "4A1C00FE00020002A00200000000000000090000000007D1D16200FFFF80" />
             </DESCRIPTORS>
              <DESCRIPTORS>
           <DESCRIPTOR name = "user_define_descriptor" data = "4A1C00FE00020003A00300000000000000090000000007D1D16200FFFF80" />
             </DESCRIPTORS>
      <LOOP ts_id = "254" original_network_id = "7340" service_id="2000">
      

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



报错是这样“无法添加名为“DESCRIPTORS”的列: 此数据表中已存在同名的嵌套表。” 请高人帮我查下原因。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,