开发者

XML and SelectNodes

A pretty newbie question as I rarely work with XML. I'm trying out writing stuff for the Subsonic API. The xml looks like this

<?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>
    <in开发者_如何学Cdex name="S">
      <artist name="Singles" id="5c5c3139322e3136382e322e31305c566f6c756d655f315c4d757369635c53696e676c6573"/>
    </index>
  </indexes>

</subsonic-response>

I'm just trying to get the index nodes.

I'm trying this, but not sure if I am doing this right. Both the SelectNodes and SelectSingleNode are returning emtpy. I'm sure I am missing something simple.

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)
{
}

I've tried with and without the namespacemanager and it's the same thing


Try using a non-empty XML namespace prefix:

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);

I've had trouble with trying to use the empty string as a (default) XML namespace prefix


How about:

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

And:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜