XML and SelectNodes



这是一个新手问题,因为我很少使用XML。我在试着为亚音速API写东西。xml看起来像这样

<?xml version="1.0" encoding="UTF-8"?>
<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.6.0">
  <indexes lastModified="1313158157783">
    <index name="A">
      <artist name="Albums" id="5c5c3139322e3136382e322e31305c566f6c756d655f315c4d757369635c416c62756d73"/>
    </index>
    <index name="S">
      <artist name="Singles" id="5c5c3139322e3136382e322e31305c566f6c756d655f315c4d757369635c53696e676c6573"/>
    </index>
  </indexes>
</subsonic-response>

我只是想得到索引节点。

我正在尝试这个,但不确定我是否做得对。SelectNodes和SelectSingleNode都返回空值。我肯定我错过了一些简单的东西。

XmlNamespaceManager nsmgr = new XmlNamespaceManager(index.NameTable);
nsmgr.AddNamespace("", "http://subsonic.org/restapi");
XmlNodeList xnList = index.SelectNodes("/subsonic-response/indexes/index", nsmgr);
XmlNode mainnode = index.SelectSingleNode("/subsonic-response", nsmgr);
foreach (XmlNode xn in xnList)
{
}

我试过使用和不使用名称空间管理器,结果是一样的

尝试使用非空XML命名空间前缀:

XmlNamespaceManager nsmgr = new XmlNamespaceManager(index.NameTable);
nsmgr.AddNamespace("x", "http://subsonic.org/restapi");
XmlNodeList xnList = index.SelectNodes("/x:subsonic-response/x:indexes/x:index", nsmgr);
XmlNode mainnode = index.SelectSingleNode("/x:subsonic-response", nsmgr);

我在尝试使用空字符串作为(默认)XML命名空间前缀

时遇到了麻烦

如何:

nsmgr.AddNamespace("x", "http://subsonic.org/restapi");

:

XmlNodeList xnList = index.SelectNodes("/x:subsonic-response/x:indexes/x:index", nsmgr);

最新更新