xml解析到Dataset报错 本帖最后由 such_beauty 于 2011-12-21 14:52:54 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你的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(); } } 浏览器能解析 单独解析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”的列: 此数据表中已存在同名的嵌套表。” 请高人帮我查下原因。 for死循环暂停的问题 关于VC中事件运算符+=(add方法)的操作问题 如何根据数据绘制曲线图? c#可以做出屏幕录制软件无法录制的窗口吗? 绑定DataGridView问题(在线等) C# 多線程---Label顯示進度 C# 欢乐碰碰球 (需要.NET Framework1.1 4322以上支持) 关于C# 3.0 学习指南一书 我在调试一源代码时,出现了如下错误,怎么也解决不了,请朋友们支持一下 我现在需要一个程序向互联网上另一个发送数据(XML),服务器端我不想用web service,因为那样还得装IIS,有其他比较简便的方式吗? 怎么下载 怎么样创建一个扩展控件程序?
浏览器直接浏览你的xml看是否能解析
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();
}
}
浏览器能解析 单独解析NIT表和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>
你自己仔细看看,明显节点不对应,浏览器根本打不开!
<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”的列: 此数据表中已存在同名的嵌套表。” 请高人帮我查下原因。