C#解析XML文件
问题描述:
我有一个问题来解析我的XML文件(RSS源)在C#中。 我只想读出“entry”条目(根父 - “feed” - 不相关)。 所有“条目”条目几乎均匀,除了“状态”部分。有些条目没有该条目。C#解析XML文件
所以我只想读出以下内容: “项” 节点:
- 更新
- 到期
- 标题
- 总结
- 状态(如果存在)
有什么建议吗? 非常感谢。
<?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>[email protected]</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>[email protected]</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>
我目前的C#代码:
public void ParseXML(XmlDocument xmlFile)
{
ArrayList updated = new ArrayList();
ArrayList expires = new ArrayList();
ArrayList title = new ArrayList();
ArrayList summary = new ArrayList();
ArrayList state = new ArrayList();
ObservableCollection<TrafficInformation> trafInfo = new ObservableCollection<TrafficInformation>();
myCollection = trafInfo;
XmlNodeReader reader = new XmlNodeReader(xmlFile);
StringBuilder output = new StringBuilder();
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
if(reader.Name == "updated")
{
updated.Add(reader.ReadString());
}
if (reader.Name == "expires")
{
expires.Add(reader.ReadString());
}
if (reader.Name == "title")
{
title.Add(reader.ReadString());
}
if (reader.Name == "summary")
{
summary.Add(reader.ReadString());
}
if (reader.Name == "state")
{
state.Add(reader.ReadString());
}
break;
}
}
}
在这种情况下,我没有数据之间的关系(如果状态不存在)。
答
您可以使用XPath表达式。下面是console-appliactaion的完整示例 - 当您使用xlmns命名空间时,它需要修改ParseXML
方法。
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表达式中,您要选择具有特定名称的所有节点,而不管它们位于何处。
如果你只想搜索<entry></entry>
元素,你可以使用下列内容:"//ns:entry/ns:updated | //ns:entry/ns:expires | //ns:entry/ns:title | //ns:entry/ns:summary | //ns:entry/ns:state"
你尝试过什么?你在哪里遇到困难?你使用的是什么版本的.NET? – Oded
我正在使用.net 4.0。如何在评论中发布格式化的代码段? – user1011394
不要在注释中发布格式化的代码 - 相反,编辑您的问题并添加细节。 – Oded