C#解析XML文件 [英] C# parse XML File
问题描述
我有一个问题,我解析XML文件(RSS订阅),在C#中。 我只是想读出项项(根父 - 饲料 - 是不相关的)。 所有入门条目几乎连,除了部分状态。某些条目不具有该条目。
I've a problem to parse my XML File (RSS Feed) in C#. I just want to read out the "entry" entries (the root parent - "feed" - is not relevant). All "entry" entries are almost even, except the part "state". Some entries doesn't have that entry.
所以,我只是想读出以下几点: 项节点:
So i just want to read out the following: "entry" nodes:
- 更新
- 过期
- 标题
- 摘要
- 在状态(如果存在)
有什么建议? 非常感谢你。
Any suggestions? Thank you very much.
<?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>
我目前的C#code:
My current C# code:
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;
}
}
}
在这种情况下,我没有数据之间的关系(如果状态不存在)。
In that case, I don't have a relationship between the data (if state doesn't exists).
推荐答案
您可以使用XPath EX pression了点。下面是在控制台上,appliactaion完整的例子 - 当您使用xlmns命名空间,它requries ParseXML
方法稍加修改
You could use XPath expression for that. Below is complete example on console-appliactaion - as you use xlmns namespace, it requries a little modification of ParseXML
method.
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中前pression手段,要选择具有特定名称的所有节点,无论他们在哪里。
// in XPath expression means, you want to select all nodes with specific name, no matter where they are.
如果您只想搜索&LT;进入&GT;&LT; /进入&GT;
元素,你可以使用下列内容:
// ns:对进入/ NS:更新| // ns:对进入/ NS:过期| // ns:对进入/ NS:标题| // ns:对进入/ NS:摘要| // ns的:进入/ NS:国家
If you want to search only <entry></entry>
elements, you can use following:
"//ns:entry/ns:updated | //ns:entry/ns:expires | //ns:entry/ns:title | //ns:entry/ns:summary | //ns:entry/ns:state"
这篇关于C#解析XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!