English 中文(简体)
阅读XML 多个NS
原标题:Reading XML File with multiple NS
  • 时间:2011-11-22 17:49:47
  •  标签:
  • c#
  • xml

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>

我只字不一,因为它很长。

我迄今为止的方法就是这样:

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

    }

我会问我:

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

我想要从这部xml法典中删除的是最后的BuildDate。 我坐在一圈子里,试图使这一法典正确。

谁能告诉我我我,我在这里做了什么错误?

谢谢!

最佳回答

你们只剩下一个要素,你可以直接使用XPath。 这一因素也存在于缺省名称空间,因此,你不需要做任何特殊的事情去做。 什么是:

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

iii

如果你确实需要把元素混入不同的名称空间,那么你也可以与XPath(例如,拿到dc:creator):

  /rss/channel/item[1]/*[local-name() =  creator ]
问题回答

你不使用名称管理人。

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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签