【问题标题】:Parse xml using c#使用 C# 解析 xml
【发布时间】:2012-03-18 12:28:19
【问题描述】:

我有一个类似的 xml

<?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>
...

我想获取元素&lt;link&gt;的属性href,其rel="http://schemas.google.com/g/2005#resumable-create-media"

我现在正在做的是获取带有xmlnode = doc.GetElementsByTagName("link"); 的节点列表,然后我将遍历所有节点并获取第一个rel="http://schemas.google.com/g/2005#resumable-create-media" 的节点

所以本质上,当我只对节点&lt;feed&gt; 的下一个子节点感兴趣且不在节点&lt;entry&gt; 内的节点时,我会遍历所有节点&lt;link&gt;。 一个&lt;feed&gt; 有多个&lt;entry&gt; 节点,而这些节点又具有多个&lt;link&gt; 标签。 相反,我只想获取&lt;link&gt; 的列表,它们直接在&lt;feed&gt; 中,而不是在&lt;entry&gt; 中。

任何帮助将不胜感激。

【问题讨论】:

    标签: c# .net google-docs google-docs-api


    【解决方案1】:

    我会使用 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)。

    【讨论】:

      【解决方案2】:

      我通常使用 System.Xml.Linq.XElement 类来处理 C# 中的 xml。如果我理解您的问题,它可能如下所示:

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

      【讨论】:

        【解决方案3】:

        LINQ To XML 似乎是一个不错的选择,因为您可以轻松解析 XML 树。

        看看官方的例子:

        http://msdn.microsoft.com/en-us/library/bb387041.aspx

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-10-07
          • 1970-01-01
          • 2021-03-27
          • 2016-08-23
          • 2013-05-29
          • 1970-01-01
          • 2015-03-14
          相关资源
          最近更新 更多