C#解析XML文件
我在用C#解析XML文件(RSS提要)时遇到问题。 我只想读出“条目”条目(根父级“提要”不相关)。 除零件“状态”外,所有“条目”条目几乎相等。有些条目没有该条目 因此,我只想读出以下内容: “入口”节点:C#解析XML文件,c#,xml,parsing,.net-4.0,C#,Xml,Parsing,.net 4.0,我在用C#解析XML文件(RSS提要)时遇到问题。 我只想读出“条目”条目(根父级“提要”不相关)。 除零件“状态”外,所有“条目”条目几乎相等。有些条目没有该条目 因此,我只想读出以下内容: “入口”节点: 更新 到期 头衔 总结 状态(如果存在) 有什么建议吗? 多谢各位 <?xml version="1.0" encoding="utf-8"?> <feed xmlns="http://www.w3.org/2005/Atom"> <updated>
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<updated>2011-01-01T00:00:00+0100</updated>
<link href="http://www.domain.com" rel="self"/>
<author>
<name>Mr X</name>
<email>Mr_X@domain.com</email>
</author>
<title>Some infos....</title>
<id>domain.com</id>
<entry>
<updated>2011-01-01T00:00:00Z</updated>
<expires>2011-01-02T00:00:00Z</expires>
<title>My first Title</title>
<id>First ID</id>
<link type="text/html" rel="alternate"
href="http://domain.com/firstElement"></link>
<summary>My first important summary</summary>
<rights>domain.com</rights>
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<div>
<img alt="second" width="32"
src="http://domain.com/firstElement.png"/>
</div>
</div>
</content>
</entry>
<entry>
<updated>2011-01-01T00:00:00Z</updated>
<expires>2011-01-02T00:00:00Z</expires>
<title>My second Title</title>
<state>active</state>
<id>Second ID</id>
<link type="text/html" rel="alternate"
href="http://domain.com/secondElement"></link>
<summary>My second important summary</summary>
<rights>domain.com</rights>
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<div>
<img alt="second" width="32"
src="http://domain.com/secondElement.png"/>
</div>
</div>
</content>
</entry>
</feed>{<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<updated>2011-01-01T00:00:00+0100</updated>
<link href="http://www.domain.com" rel="self"/>
<author>
<name>Mr X</name>
<email>Mr_X@domain.com</email>
</author>
<title>Some infos....</title>
<id>domain.com</id>
<entry>
<updated>2011-01-01T00:00:00Z</updated>
<expires>2011-01-02T00:00:00Z</expires>
<title>My first Title</title>
<id>First ID</id>
<link type="text/html" rel="alternate"
href="http://domain.com/firstElement"></link>
<summary>My first important summary</summary>
<rights>domain.com</rights>
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<div>
<img alt="second" width="32"
src="http://domain.com/firstElement.png"/>
</div>
</div>
</content>
</entry>
<entry>
<updated>2011-01-01T00:00:00Z</updated>
<expires>2011-01-02T00:00:00Z</expires>
<title>My second Title</title>
<state>active</state>
<id>Second ID</id>
<link type="text/html" rel="alternate"
href="http://domain.com/secondElement"></link>
<summary>My second important summary</summary>
<rights>domain.com</rights>
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<div>
<img alt="second" width="32"
src="http://domain.com/secondElement.png"/>
</div>
</div>
</content>
</entry>
</feed>
2011-01-01T00:00:00+0100
X先生
先生_X@domain.com
一些信息。。。。
domain.com
2011-01-01T00:00:00Z
2011-01-02T00:00:00Z
我的第一个头衔
第一身份证
我的第一个重要总结
domain.com
2011-01-01T00:00:00Z
2011-01-02T00:00:00Z
我的第二个头衔
积极的
第二身份证
我的第二个重要总结
domain.com
{
2011-01-01T00:00:00+0100
X先生
先生_X@domain.com
一些信息。。。。
domain.com
2011-01-01T00:00:00Z
2011-01-02T00:00:00Z
我的第一个头衔
第一身份证
我的第一个重要总结
domain.com
2011-01-01T00:00:00Z
2011-01-02T00:00:00Z
我的第二个头衔
积极的
第二身份证
我的第二个重要总结
domain.com
我当前的C#代码:
publicvoidparsexml(xmldocumentxmlfile)
{
ArrayList updated=新的ArrayList();
ArrayList expires=新建ArrayList();
ArrayList title=新的ArrayList();
ArrayList summary=新的ArrayList();
ArrayList状态=新的ArrayList();
ObservableCollection TraffInfo=新的ObservableCollection();
myCollection=traffinfo;
XmlNodeReader reader=新的XmlNodeReader(xmlFile);
StringBuilder输出=新的StringBuilder();
while(reader.Read())
{
开关(reader.NodeType)
{
case XmlNodeType.Element:
如果(reader.Name==“已更新”)
{
updated.Add(reader.ReadString());
}
如果(reader.Name==“expires”)
{
expires.Add(reader.ReadString());
}
如果(reader.Name==“title”)
{
title.Add(reader.ReadString());
}
如果(reader.Name==“摘要”)
{
Add(reader.ReadString());
}
如果(reader.Name==“state”)
{
state.Add(reader.ReadString());
}
打破
}
}
}
在这种情况下,数据之间没有关系(如果状态不存在).我认为直接解析XML最简单的方法是使用LINQ-to-XML。您可以找到更多信息。您可以使用XPath表达式来实现这一点。下面是控制台appliactaion上的完整示例-当您使用xlmns命名空间时,需要对
解析XML
方法进行一些修改
using System;
using System.Xml;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load("XMLFile1.xml");
XmlNamespaceManager xmlnm = new XmlNamespaceManager(xmlDocument.NameTable);
xmlnm.AddNamespace("ns", "http://www.w3.org/2005/Atom");
ParseXML(xmlDocument, xmlnm);
Console.WriteLine("\n---XML parsed---");
Console.ReadKey();
}
public static void ParseXML(XmlDocument xmlFile, XmlNamespaceManager xmlnm)
{
XmlNodeList nodes = xmlFile.SelectNodes("//ns:updated | //ns:expires | //ns:title | //ns:summary | //ns:state", xmlnm);
foreach (XmlNode node in nodes)
{
Console.WriteLine(node.Name + " = " + node.InnerXml);
}
}
}
}
//在XPath表达式中,您希望选择具有特定名称的所有节点,无论它们位于何处
如果只想搜索
元素,可以使用以下命令:“//ns:entry/ns:updated |//ns:entry/ns:expires |//ns:entry/ns:title |//ns:entry/ns:summary |//ns:entry/ns:state”
您尝试过什么?哪里有困难?您使用的是什么版本的.NET?我使用的是.NET 4.0。如何在评论中发布格式化代码段?不要在评论中发布格式化代码-而是编辑您的问题并添加细节。请不要使用ArrayList
-而是使用List
。这样,yo你至少有类型安全。好的,我会记住这个^^。我发现这对于开始使用linq to xml更有用:
using System;
using System.Xml;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load("XMLFile1.xml");
XmlNamespaceManager xmlnm = new XmlNamespaceManager(xmlDocument.NameTable);
xmlnm.AddNamespace("ns", "http://www.w3.org/2005/Atom");
ParseXML(xmlDocument, xmlnm);
Console.WriteLine("\n---XML parsed---");
Console.ReadKey();
}
public static void ParseXML(XmlDocument xmlFile, XmlNamespaceManager xmlnm)
{
XmlNodeList nodes = xmlFile.SelectNodes("//ns:updated | //ns:expires | //ns:title | //ns:summary | //ns:state", xmlnm);
foreach (XmlNode node in nodes)
{
Console.WriteLine(node.Name + " = " + node.InnerXml);
}
}
}
}