2010-11-18 38 views
1

我目前使用DOM在C#项目中导航xml。但是,我最近遇到的一些XML有些不同。用C#/ DOM读取不同的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条目条目的SelectSingleNode(/内容/ somefield1 /子),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(/原子:内容/ somefield1 /子)肯定是行不通的......有什么建议?

回答

1

atom:只是命名空间,可能你可能会忽略它。如果仍然没有工作,你可能需要使用:

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

selectsinglenode("atom:content/somefield1/subfield", nsmgr); 

这是记录here

+0

卫生署!我没有意识到这是一个标准的命名空间!非常感谢! – 2010-11-18 02:46:10