问题描述
我正在交易以访问XML文档的特定节点.我意识到这是一个基本名称空间.这是一个例子.
我有兴趣从所有后代节点(entry)中获取节点d:mediaurl的值.我还没有完成.
当我调试变量迭代器'i'时,我可以看到XML再次包含默认名称空间,例如:
<entry xmlns="http://schemas.microsoft.com.ado/..."
,我还必须包括另一个名为" D"的名称空间.
我该怎么办才能访问该特定节点?
这就是我所拥有的.
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中与XML中的名称空间打交道的方式).对我来说,提前设置名称空间,然后使用Element(NS + "element name") than to use xname.get (though using xname.get'的努力较少.
如果您想获得每个条目的所有"媒体"元素,那么我会做这样的事情:
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();
如果您只想获得其中一个,那么您需要做一些不同的事情,具体取决于您想要得到的.
用于属性媒体:
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();
对于缩略图媒体:
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();
这里的关键是将命名空间与元素名称结合使用以检索它.
其他推荐答案
var query = from i in doc.Descendants("{full namespace for prefix d}MediaUrl") select new Image() { Url = i.Value };
问题描述
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.
I'm interested to get the value of the node d:MediaUrl from all descendents node (entry). And I haven't accomplished that.
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/..."
And also I have to include the another namespace called 'd'.
What can I do to access to that particular nodes?
This is what I have.
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();
推荐答案
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.
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.
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();
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.
其他推荐答案
var query = from i in doc.Descendants("{full namespace for prefix d}MediaUrl") select new Image() { Url = i.Value };