现在的位置: 首页 > 综合 > 正文

C# XmlDocument SelectNodes issue

2013年10月03日 ⁄ 综合 ⁄ 共 1287字 ⁄ 字号 评论关闭

 Hi all,

I have a very simple xspf file:

<?xml version="1.0"?>
<playlist version="1" xmlns="http://xspf.org/ns/0/">
    <title>Easter Mix</title>
    <info>This is our selection for the Easter period.</info>
    <trackList>
        <track>
            <info>some info</info>
            <creator>some creator</creator>
            <title>a title</title>
            <location>location1.mp3</location>
            <image>some.jpg</image>
        </track>
        <track>
            <info>some info</info>
            <creator>some creator</creator>
            <title>a title</title>
            <location>location2.mp3</location>
            <image>some.jpg</image>
        </track>
    </trackList>
</playlist>

 

I am trying to get the Title and Info elements at the top of the document using

xmldoc.SelectNodes("/playlist/title")

or 

 xmldoc.SelectSingleNode("/playlist/title")

But it just doesn't find it.Anybody knows how should I do it?

Many thanks

 

 

Not sure what language you are coding in but your problem is you have a namespace in your Xml and you are not querying the Xml using it.  Here is some sample code that shows you how to add a namespace to solve the problem

XmlDocument xmldoc = new XmlDocument();

xmldoc.Load("Playlist.xml");
XmlNamespaceManager manager = new XmlNamespaceManager(xmldoc.NameTable);
manager.AddNamespace("xspf", "http://xspf.org/ns/0/");

string title = xmldoc.SelectSingleNode("//xspf:title", manager).InnerText

抱歉!评论已关闭.