asp.net解析XML,请高手解析(C#),只要能解析出来就得分,请看xml文件

nixiang2008 2008-07-31 09:59:32
<?xml version="1.0" encoding="gb2312"?>
<xinwen>
<news news_id="5" news_title="XML系统" news_author="邓为" news_ly="原创" news_content="fdgvdf" news_adddate="2008-1-12 15:39:24">
<c a="1" b="2" e="3" f="4">
<c a="1" b="2" e="3" f="4">
<c a="1" b="2" e="3" f="4">
<c a="1" b="2" e="3" f="4">
<c a="1" b="2" e="3" f="4">
<c a="1" b="2" e="3" f="4">
<news>
<news news_id="5" news_title="XML系统" news_author="邓为" news_ly="原创" news_content="fdgvdf" news_adddate="2008-1-12 15:39:24">
<c a="1" b="2" e="3" f="4">
<c a="1" b="2" e="3" f="4">
<c a="1" b="2" e="3" f="4">
<c a="1" b="2" e="3" f="4">
<c a="1" b="2" e="3" f="4">
<c a="1" b="2" e="3" f="4">
<news>
</xinwen>
请高手帮助我吧这个XML的属性给解析出来!!
...全文
782 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
nixiang2008 2008-07-31
  • 打赏
  • 举报
回复
DOM解析和XmlReader有什么大的区别?如果用XmlReader解析的话,该如何解析!
lovefootball 2008-07-31
  • 打赏
  • 举报
回复
如果文件比较大
建议使用XmlReader的方式来读取
不要使用DOM的方式
nixiang2008 2008-07-31
  • 打赏
  • 举报
回复
我还想要问下,就是如果解析XML资源比较大的话,什么方法最好?
nixiang2008 2008-07-31
  • 打赏
  • 举报
回复
各位高手说的是啊,xml没有一个结束标记;我来测试下各位的代码,==在联系啊
symbol441 2008-07-31
  • 打赏
  • 举报
回复
对于这种顺序读取,建议采用XmlReader
参考

XmlTextReader reader = new XmlTextReader(Server.MapPath("News.xml"));
while (reader.Read())
{

switch (reader.NodeType)
{

case XmlNodeType.Element:
Response.Write("节点名称:" + reader.LocalName + "<br>");
for (int i = 0; i < reader.AttributeCount; i++)
{
reader.MoveToAttribute(i);
Response.Write("属性:" + reader.Name + "=" + reader.Value + " ");
Response.Write("<br />");
}
break;

}
}
reader.Close();


begonia_ref 2008-07-31
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 bsetsail 的回复:]
C# codeXmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(TextBox3.Text);
XmlNode xmlnode1 = xmldoc.SelectSingleNode("xinwen");
foreach (XmlElement xmlnode in xmlnode1.ChildNodes)
{
Response.Write("news_id=" + xmlnode.GetAttribute("news_id"));
Response.Write("news_title=" + xmlnode.GetAttribute("news_title"));
Response.Write("news_author=" + xmlnode.GetAttribute("news_auth…
[/Quote]
begonia_ref 2008-07-31
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 bsetsail 的回复:]
C# codeXmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(TextBox3.Text);
XmlNode xmlnode1 = xmldoc.SelectSingleNode("xinwen");
foreach (XmlElement xmlnode in xmlnode1.ChildNodes)
{
Response.Write("news_id=" + xmlnode.GetAttribute("news_id"));
Response.Write("news_title=" + xmlnode.GetAttribute("news_title"));
Response.Write("news_author=" + xmlnode.GetAttribute("news_auth…
[/Quote]
雪狐 2008-07-31
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 bsetsail 的回复:]
C# codeXmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(TextBox3.Text);
XmlNode xmlnode1 = xmldoc.SelectSingleNode("xinwen");
foreach (XmlElement xmlnode in xmlnode1.ChildNodes)
{
Response.Write("news_id=" + xmlnode.GetAttribute("news_id"));
Response.Write("news_title=" + xmlnode.GetAttribute("news_title"));
Response.Write("news_author=" + xmlnode.GetAttribute("news_auth…
[/Quote]
bsetsail 2008-07-31
  • 打赏
  • 举报
回复
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(TextBox3.Text);
XmlNode xmlnode1 = xmldoc.SelectSingleNode("xinwen");
foreach (XmlElement xmlnode in xmlnode1.ChildNodes)
{
Response.Write("news_id=" + xmlnode.GetAttribute("news_id"));
Response.Write("news_title=" + xmlnode.GetAttribute("news_title"));
Response.Write("news_author=" + xmlnode.GetAttribute("news_author"));
Response.Write("news_ly=" + xmlnode.GetAttribute("news_ly"));
Response.Write("news_content=" + xmlnode.GetAttribute("news_content"));
Response.Write("news_adddate=" + xmlnode.GetAttribute("news_adddate"));
Response.Write("<br>");
foreach (XmlElement xmlele1 in xmlnode.ChildNodes)
{
Response.Write("a=" + xmlele1.GetAttribute("a"));
Response.Write("b=" + xmlele1.GetAttribute("b"));
Response.Write("e=" + xmlele1.GetAttribute("e"));
Response.Write("f=" + xmlele1.GetAttribute("f"));
}
}
bsetsail 2008-07-31
  • 打赏
  • 举报
回复
大哥
你的xml没有一个结束标记
写成
<?xml version="1.0" encoding="gb2312"?> 
<xinwen>
<news news_id="5" news_title="XML系统" news_author="邓为" news_ly="原创" news_content="fdgvdf" news_adddate="2008-1-12 15:39:24">
<c a="1" b="2" e="3" f="4"/>
<c a="1" b="2" e="3" f="4"/>
<c a="1" b="2" e="3" f="4"/>
<c a="1" b="2" e="3" f="4"/>
<c a="1" b="2" e="3" f="4"/>
<c a="1" b="2" e="3" f="4"/>
</news>
<news news_id="5" news_title="XML系统" news_author="邓为" news_ly="原创" news_content="fdgvdf" news_adddate="2008-1-12 15:39:24">
<c a="1" b="2" e="3" f="4"/>
<c a="1" b="2" e="3" f="4"/>
<c a="1" b="2" e="3" f="4"/>
<c a="1" b="2" e="3" f="4"/>
<c a="1" b="2" e="3" f="4"/>
<c a="1" b="2" e="3" f="4"/>
</news>
</xinwen>
wszhoho 2008-07-31
  • 打赏
  • 举报
回复
这个xml本身格式就不对,应该为:
<?xml version="1.0" encoding="gb2312"?>
<xinwen>
<news news_id="5" news_title="XML系统" news_author="邓为" news_ly="原创" news_content="fdgvdf" news_adddate="2008-1-12 15:39:24">
<c a="1" b="2" e="3" f="4"/>
<c a="1" b="2" e="3" f="4"/>
<c a="1" b="2" e="3" f="4"/>
<c a="1" b="2" e="3" f="4"/>
<c a="1" b="2" e="3" f="4"/>
<c a="1" b="2" e="3" f="4"/>
</news>
<news news_id="5" news_title="XML系统" news_author="邓为" news_ly="原创" news_content="fdgvdf" news_adddate="2008-1-12 15:39:24">
<c a="1" b="2" e="3" f="4"/>
<c a="1" b="2" e="3" f="4"/>
<c a="1" b="2" e="3" f="4"/>
<c a="1" b="2" e="3" f="4"/>
<c a="1" b="2" e="3" f="4"/>
<c a="1" b="2" e="3" f="4"/>
</news>
</xinwen>


读取的:
 DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("~/tt.xml"));
string a = "";
for (int i = 0; i < ds.Tables["news"].Rows.Count; i++)
{
for (int j = 0; j < ds.Tables["news"].Columns.Count; j++)
{
a = a + ds.Tables["news"].Rows[i][j].ToString() + ",";
}
if (a.Length > 0) a = a.Substring(0, a.Length - 1);
a = a + "<br />";
}
for (int i = 0; i < ds.Tables["c"].Rows.Count; i++)
{
for (int j = 0; j < ds.Tables["c"].Columns.Count; j++)
{
a = a + ds.Tables["c"].Rows[i][j].ToString() + ",";
}
if (a.Length > 0) a = a.Substring(0, a.Length - 1);
a = a + "<br />";
}

Response.Write(a);
nixiang2008 2008-07-31
  • 打赏
  • 举报
回复
只要解析显示出来即可
hhhh63 2008-07-31
  • 打赏
  • 举报
回复
如果是存数据库,存入数组,都是用同样的方法。
hhhh63 2008-07-31
  • 打赏
  • 举报
回复
不知道你想解析成什么?如果要按一定的格式显示出来,可以用下面的方法
XmlDocument doc = new XmlDocument();
doc.Load("http://www.ibfx.com/ticker.aspx");
XmlNodeList sectionNodeList = doc.GetElementsByTagName("currency");

int n = sectionNodeList.Count;
for( int i = 0; i < n; i++ )
{
string symbol = sectionNodeList[i].Attributes["symbol"].Value;
string bid = sectionNodeList[i].Attributes["bid"].Value;
string ask = sectionNodeList[i].Attributes["ask"].Value;
int dig = comm.PriceDig( symbol );

然后用正则表达式取出a="1" b="2" e="3" f="4"
贝克汉鸭 2008-07-31
  • 打赏
  • 举报
回复
什么叫 把xml的属性解析出来,那个元素的什么属性?说明白点
nixiang2008 2008-07-31
  • 打赏
  • 举报
回复
<?xml version="1.0" encoding="gb2312"?>
<xinwen>
<news news_id="5" news_title="XML系统" news_author="邓为" news_ly="原创" news_content="fdgvdf" news_adddate="2008-1-12 15:39:24">
<c a="1" b="2" e="3" f="4">
<c a="1" b="2" e="3" f="4">
<c a="1" b="2" e="3" f="4">
<c a="1" b="2" e="3" f="4">
<c a="1" b="2" e="3" f="4">
<c a="1" b="2" e="3" f="4">
</news>
<news news_id="5" news_title="XML系统" news_author="邓为" news_ly="原创" news_content="fdgvdf" news_adddate="2008-1-12 15:39:24">
<c a="1" b="2" e="3" f="4">
<c a="1" b="2" e="3" f="4">
<c a="1" b="2" e="3" f="4">
<c a="1" b="2" e="3" f="4">
<c a="1" b="2" e="3" f="4">
<c a="1" b="2" e="3" f="4">
</news>
</xinwen>
jl_lsj 2008-07-31
  • 打赏
  • 举报
回复
帮顶

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧