LINQ to XML - 使用前缀访问后代
我有一个像这样的示例 xml 文件
<vs:BioData>
<vs:Name>Name</vs:Name>
<vs:Address>address</vs:Address>
<vs:Zip>Zip</vs:zip>
</vs:BioData>
所有节点都有一个前缀值 vs ,谁能告诉我如何解析这个文件以读取名称和地址信息?我对 LINQ 很陌生。对此的任何帮助将不胜感激。
谢谢!
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>
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.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要知道名称空间是什么。这将在前面声明,类似于:
您可以使用
XNamespace
进行查询:这里的
+
实际上是将XNamespace
和字符串转换为一个XName
。You need to know what the namespace is. That will have been declared earlier, with something like:
You can query using
XNamespace
:The
+
here is actually converting anXNamespace
and a string to anXName
.前缀“vs”应映射到 XML 文档标头中的命名空间,例如:
然后您可以使用 XNamespace 通过 LINQ 选择这些元素,如下所示:
XNamespace 和 XName 类型都支持从字符串隐式转换。
The prefix "vs" should be mapped to a namespace in the header of the XML document, eg:
Then you can select those elements with LINQ by using an XNamespace, like this:
The XNamespace and XName types all support implicit conversion from strings.
我知道这篇文章确实很旧,但想提供一些额外的信息。我在寻求有关更复杂的 xml 文件的帮助时被定向到这个问题和答案。我的 xml 有多个名称空间,并且也有嵌套的名称空间。我能够通过在此找到的建议解决我的问题 链接
很有帮助,想将其发布在这里,以防其他人遇到同样的问题
I know this post is really old but wanted to provide some additional information. I was directed to this question and answer while seeking help with a more complex xml file. My xml had several names spaces and had nested namespaces as well. I was able to solve my problem with the suggestions found at this link
It was great help and wanted to post it here in case someone else ran into the same problem