LINQ到XML - 用一个前缀访问后裔 [英] LINQ to XML - accessing descendants with a prefix

查看:106
本文介绍了LINQ到XML - 用一个前缀访问后裔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样一个示例XML文件

I have a sample xml file like this

<vs:BioData>
<vs:Name>Name</vs:Name>
<vs:Address>address</vs:Address>
<vs:Zip>Zip</vs:zip>
</vs:BioData>



所有节点都有一个前缀值VS,任何人都可以告诉我,我将如何去分析这个文件来读取姓名和地址信息?我是很新的LINQ。任何的帮助将不胜感激。

All the nodes have a prefix value as vs and can anyone tell me how would I go about parsing this file to read Name and Address Information? I am very new to LINQ. Any help on this would be greatly appreciated.

谢谢!

推荐答案

您需要知道名称空间是什么。这将已经早些时候宣布,与类似:

You need to know what the namespace is. That will have been declared earlier, with something like:

xmlns:vs="http://some_url_here"

您可以查询使用的XNamespace

XNamespace vs = "http://some_url_here";
var names = doc.Descendants(vs + "Name")
               .Select(x => (string) x)
               .ToList();



+ 这里实际上是转换一个的XNamespace 和一个字符串到的XName

The + here is actually converting an XNamespace and a string to an XName.

这篇关于LINQ到XML - 用一个前缀访问后裔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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