【问题标题】:Reading different XML with C#/DOM使用 C#/DOM 读取不同的 XML
【发布时间】:2010-11-18 02:20:37
【问题描述】:

我目前正在使用 DOM 在我的 C# 项目中导航 xml。但是,我最近遇到的一些 XML 有点不同。

而我通常有:

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <entry>
    <author>
      <name>Me =)</name>
    </author>
    <content>
      <somefield1>
        <Subfield>subfield data</subfield>
      </somefield>
    </content>
  </entry>
</feed>

并且可以使用foreach条目作为条目进行导航,选择singlenode(/content/somefield1/subfield),innertext从每个条目的子字段中获取数据,新的XML如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<atom:feed xmlns:atom="http://www.w3.org/2005/Atom">
    <atom:entry>
        <atom:author>
            <name>Me =)</name>
        </atom:author>
        <atom:content>
          <somefield1>
            <Subfield>subfield data</subfield>
          </somefield>
        </atom:content>
    </atom:entry>
</atom:feed>

selectsinglenode(/atom:content/somefield1/subfield) 肯定行不通...有什么建议吗?

【问题讨论】:

    标签: c# xml


    【解决方案1】:

    atom: 只是名称空间,您可能会忽略它。如果还是不行,你可能需要使用:

    XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
    nsmgr.AddNamespace("atom", "http://www.w3.org/2005/Atom");
    
    selectsinglenode("atom:content/somefield1/subfield", nsmgr);
    

    记录在案的here

    【讨论】:

    • 哇!我没有意识到这是一个标准的命名空间!非常感谢!
    猜你喜欢
    • 2011-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多