简体   繁体   中英

Reading XML File with multiple NS

I am trying to read an XML feed to get the last post date. My xml looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
    xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:wfw="http://wellformedweb.org/CommentAPI/"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:atom="http://www.w3.org/2005/Atom"
    xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
    xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
    >

<channel>
    <title>mysite</title>
    <atom:link href="http://www.mysite.com/news/feed/" rel="self" type="application/rss+xml" />
    <link>http://www.mysite.com/news</link>
    <description>mysite</description>
    <lastBuildDate>Tue, 22 Nov 2011 16:10:27 +0000</lastBuildDate>
    <language>en</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <generator>http://wordpress.org/?v=3.0.4</generator>
        <item>
        <title>My first post!</title>
        <link>http://www.mysite.com/news/2011/11/22/docstore-v2-released/</link>
        <comments>http://www.mysite.com/news/2011/11/22/docstore-v2-released/#comments</comments>
        <pubDate>Tue, 22 Nov 2011 16:10:27 +0000</pubDate>
        <dc:creator>mysite</dc:creator>
                <category><![CDATA[News]]></category>
        <category><![CDATA[Promotions]]></category>
        <category><![CDATA[docstore]]></category>

I didn't show all of the xml since it is rather long.

My method, so far, looks like this:

    private void button1_Click(object sender, EventArgs e)
    {

        var XmlDoc = new XmlDocument();

        // setup the XML namespace manager
        var mgr = new XmlNamespaceManager(XmlDoc.NameTable);

        // add the relevant namespaces to the XML namespace manager
        mgr.AddNamespace("ns", "http://purl.org/rss/1.0/modules/content/");

        var webClient = new WebClient();
        var stream = new MemoryStream(webClient.DownloadData("http://www.mysite.com/news/feed/"));
        XmlDoc.Load(stream);

        // **USE** the XML anemspace in your XPath !!
        XmlElement NodePath = (XmlElement)XmlDoc.SelectSingleNode("/ns:Response");

        while (NodePath != null)
        {
            foreach (XmlNode Xml_Node in NodePath)
            {
                Console.WriteLine(Xml_Node.Name + ": " + Xml_Node.InnerText);
            }
        }

    }

I'm having a problem with it telling me:

Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.

All I want to pull out of this xml code is the 'lastBuildDate'. I'm going in circles trying to get this code right.

Can someone tell me what I am doing wrong here?

Thank you!

You're not using the namespace manager.

// **USE** the XML anemspace in your XPath !!         
XmlElement NodePath = (XmlElement)XmlDoc.SelectSingleNode("/ns:Response", mgr); 

There is only one of the element you are going after, you could go directly to it using the XPath. That element is also in the default namespace, so you do not need to do anything special to get to it. What about:

var XPATH_BUILD_DATE="/rss/channel/lastBuildDate";


private void button1_Click(object sender, EventArgs e){
  var xmlDoc = new XmlDocument();
  var webClient = new WebClient();
  var stream = new MemoryStream(webClient.DownloadData("http://www.mysite.com/news/feed/"));
xmlDoc.Load(stream);

XmlElement xmlNode = (XmlElement)xmlDoc.SelectSingleNode(XPATH_BUILD_DATE);

Console.WriteLine(xmlNode.Name + ": " + xmlNode.InnerText);       

}

If you did however need to dig into elements in a different namespace, you can do that also with the XPath (example, getting the dc:creator:

  /rss/channel/item[1]/*[local-name() = 'creator']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM