C # анализировать XML-файл
У меня проблема с анализом моего XML-файла (RSS-канал) в C #. Я просто хочу зачитать записи «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>{<?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;
}
}
}
В этом случае у меня нет связи между данными (если состояние не существует).