xml解析到Dataset报错
时间:2011-12-21
来源:互联网
我需要将一xml文档解析到dataset中,遇到DuplicateNameException 的报错,请问如何能将这个xml里的信息读出来。
xml文件
XML code
C#的代码是这样
C# code
我的任务是将network_id 里的内容读出来并展现。
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看是否能解析
浏览器直接浏览你的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();
}
}
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看是否能解析
你的xml符合xml规则吗?
浏览器直接浏览你的xml看是否能解析
浏览器能解析 单独解析NIT表和BAT表都能成功,但是放在一起就失败了。
作者: such_beauty 发布时间: 2011-12-21
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28