如何在带有名称空间的xml中获取特定节点? [英] How to get an specific node in xml with namespaces?

查看:64
本文介绍了如何在带有名称空间的xml中获取特定节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理要从XML文档访问特定节点的问题.我意识到这是一个基本的命名空间.这是示例.

I'm dealing to access an specific node from a XML Document. I realized that this one as a base namespace. Here is the example.

我有兴趣从所有后代节点(条目)中获取节点d:MediaUrl的值.而且我还没有做到.

I'm interested to get the value of the node d:MediaUrl from all descendents node (entry). And I haven't accomplished that.

当我调试变量迭代器'i'时,我可以看到XML再次包含默认名称空间,如下所示:

When I debug the variable iterator 'i', I can see that the XML includes the default namespace again, something like:

<entry xmlns="http://schemas.microsoft.com.ado/..." 

而且我还必须包括另一个名为'd'的命名空间.

And also I have to include the another namespace called 'd'.

我该怎么做才能访问该特定节点?

What can I do to access to that particular nodes?

这就是我所拥有的.

        var doc = XDocument.Parse(result);

        string BASE_NS = "http://www.w3.org/2005/Atom";

        string d = "http://schemas.microsoft.com/ado/2007/08/dataservices";
        var query = from i in doc.Descendants(XName.Get("entry", BASE_NS))
                    select new Image()
        {
            Url = i.Element(XName.Get("MediaUrl", BASE_NS)).Value
        };

        var results = query.ToList();

推荐答案

我建议使用XNamespace而不是XName(主要是个人喜好,因为这就是我一直在LINQ to XML中处理名称空间的方式) .对我来说,如果您要这样做,那么先设置名称空间然后再使用Element(NS + "element name") than to use XName.Get (though using XName.Get`就可以了.

I would suggest using XNamespace rather than XName (personal preference, mainly - as that's how I've always dealt with namespaces in LINQ to XML). To me it's less effort to set up the namespaces in advance and then use Element(NS + "element name") than to useXName.Get(though usingXName.Get` is perfectly fine if that's what you want to do.

如果您想获取每个条目的所有"MediaUrl"元素,那么我将执行以下操作:

If you want to get a all the "MediaUrl" elements for each entry, then I'd do something like this:

XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices";

var query = (from i in doc.Descendants(d + "MediaUrl")
             select new Image()
             {
                 Url = i.Value
             }).ToList();

如果您只想获得其中一个,那么您需要做一些不同的事情,具体取决于您想获得哪一个.

If you want to get only one of them, then you need to do something a little different, depending on which one you wanted to get.

对于属性MediaUrl:

For the properties MediaUrl:

XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices";
XNamespace m = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";

var query = (from i in doc.Descendants(m + "properties")
             select new Image()
             {
                 Url = i.Element(d + "MediaUrl").Value
             }).ToList();

对于缩略图MediaUrl:

For the Thumbnail MediaUrl:

XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices";

var query = (from i in doc.Descendants(d + "Thumbnail")
             select new Image()
             {
                 Url = i.Element(d + "MediaUrl").Value
             }).ToList();

此处的关键是将名称空间与元素名称结合使用以进行检索.

The key here is to use the namespace in conjunction with the element name in order to retrieve it.

这篇关于如何在带有名称空间的xml中获取特定节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆