试图获取Xelement的xml中元素的值

时间:2022-11-28 00:21:12

I have an Xelement being passed into a function:

我有一个Xelement传递给一个函数:

ByVal request As XElement

inside the Xelement object is an element:

Xelement对象内部是一个元素:

<nc:ID>1234567</nc:ID>

I should be able to get this value by:

我应该能够通过以下方式获得此值:

request.Element("ID").value

...but it does not return anything. I suspect this is because of the namespace prefix. I saw a solution in this post which after translating to VB.net (request.Elements().Where(Function(e) e.Name.LocalName = "ID").Value) works, but I do not understand why or why the .element("ID").value does not return the value. Can anyone give me some insight into this?

......但它没有返回任何东西。我怀疑这是因为名称空间前缀。我在这篇文章中看到了一个解决方案,它转换为VB.net(request.Elements()。其中​​(Function(e)e.Name.LocalName =“ID”)。Value)有效,但我不明白为什么或为什么.element(“ID”)。value不返回值。任何人都可以给我一些洞察力吗?

1 个解决方案

#1


0  

I ran into the same problem today. Apparently "nc" is not the namespace, but is in fact the namespace prefix; you need to get the namespace itself. Here's how it turns out it has to be done:

我今天遇到了同样的问题。显然“nc”不是名称空间,而实际上是名称空间前缀;你需要获得名称空间本身。以下是它必须要做的事情:

XNamespace ns = request.GetNamespaceOfPrefix("nc");
XElement ID = request.Element(ns + "ID");

#1


0  

I ran into the same problem today. Apparently "nc" is not the namespace, but is in fact the namespace prefix; you need to get the namespace itself. Here's how it turns out it has to be done:

我今天遇到了同样的问题。显然“nc”不是名称空间,而实际上是名称空间前缀;你需要获得名称空间本身。以下是它必须要做的事情:

XNamespace ns = request.GetNamespaceOfPrefix("nc");
XElement ID = request.Element(ns + "ID");