2012-03-18 3 views
2

У меня есть XML, какАнализировать XML с помощью C#

<?xml version='1.0' encoding='UTF-8'?> 
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/' xmlns:docs='http://schemas.google.com/docs/2007' xmlns:batch='http://schemas.google.com/gdata/batch' xmlns:gd='http://schemas.google.com/g/2005' gd:etag='W/&quot;C0EARXY8eit7ImA9WhVREE0.&quot;'> 

<id>https://docs.google.com/feeds/default/private/full</id> 
<updated>2012-03-17T16:27:24.872Z</updated> 
<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/docs/2007#item' label='item'/> 

<title>Available Documents - </title> 

<link rel='alternate' type='text/html' href='https://docs.google.com'/> 
<link rel='http://schemas.google.com/g/2005#resumable-create-media' type='application/atom+xml' href='https://docs.google.com/feeds/upload/create-session/default/private/full'/> 
<link rel='http://schemas.google.com/docs/2007#alt-post' type='application/atom+xml' href='https://docs.google.com/feeds/upload/file/default/private/full'/> 
<link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='https://docs.google.com/feeds/default/private/full'/> 
<link rel='http://schemas.google.com/g/2005#post' type='application/atom+xml' href='https://docs.google.com/feeds/default/private/full'/> 
<link rel='http://schemas.google.com/g/2005#batch' type='application/atom+xml' href='https://docs.google.com/feeds/default/private/full/batch'/> 
<link rel='self' type='application/atom+xml' href='https://docs.google.com/feeds/default/private/full/-/document'/> 

<some more tags /> 

<entry gd:etag='&quot;E0UXTh9YDSt7ImBr&quot;'> 
<some more tags /> 
<title>hi</title> 
<link rel='http://schemas.google.com/docs/2007#parent' type='application/atom+xml' href=''/> 
<link rel='alternate' type='application/atom+xml' href=''/> 
<link rel='http://schemas.google.com/docs/2007#embed' type='application/atom+xml' href=''/> 
<link rel='http://schemas.google.com/docs/2007#icon' type='application/atom+xml' href=''/> 
<link rel='http://schemas.google.com/g/2005#resumable-edit-media' type='application/atom+xml' href=''/> 
<link rel='http://schemas.google.com/docs/2007#alt-edit-media' type='application/atom+xml' href=''/> 
<link rel='http://schemas.google.com/docs/2007/thumbnail' type='application/atom+xml' href=''/> 
<link rel='self' type='application/atom+xml' href=''/> 
<link rel='edit' type='application/atom+xml' href=''/> 
<link rel='edit-media' type='application/atom+xml' href=''/> 
... 
<some more tags /> 
... 
</entry> 
<entry> 
... 
</entry> 
... 

Я хочу, чтобы принести атрибут href элемента <link> которого rel="http://schemas.google.com/g/2005#resumable-create-media"

Что я делаю сейчас я список становится узлы с xmlnode = doc.GetElementsByTagName("link"); тогда я перебором все из них и извлечения первого чьего rel="http://schemas.google.com/g/2005#resumable-create-media"

Поэтому в основном я перекручивание через все узлы <link> когда я JUS t интересуются тем, которые являются следующим потомком узла <feed> и не находятся внутри узла <entry>. A <feed> имеют несколько узлов <entry>, которые в свою очередь имеют несколько тегов <link>. И вместо этого я хотел бы просто получить список <link>, которые находятся непосредственно в <feed>, а не в <entry>.

Любая помощь будет оценена по достоинству.

ответ

1

Обычно я использую классы System.Xml.Linq.XElement для работы с xml в C#. Если я понимаю вашу проблему, это может выглядеть так:

XElement input = XElement.Load(filename); 
foreach(XElement feedChild in input.Elements("feed")) 
    foreach(XElement linkChild in feedChild.Elements("link")) 
3

Я хотел бы использовать XPath:

var nodes = xml.SelectNodes("//link[@rel='http://schemas.google.com/g/2005#resumable-create-media']", namespaceManaager) 

(где namespaceManaager является XmlNamespaceManager сконфигурировано для отображения тегов a к пространству имен http://www.w3.org/2005/Atom).

Смежные вопросы