ASP.NET Web 服务返回 XML 结果且节点值始终为 null

发布于 2024-09-02 22:38:36 字数 765 浏览 1 评论 0原文

我有一个返回 XMLDocument 的 ASP.NET Web 服务。该 Web 服务是使用 XMLHttpRequest 从 Firefox 扩展调用的。

var serviceRequest = new XMLHttpRequest();
serviecRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

我使用responseXML 使用结果。到目前为止,一切都很好。但是,当我迭代 XML 时,我检索了 nodeValue - nodeValue 始终为 null。当我检查 nodeType 时,nodeType 是类型 1 (Node.ELEMENT_NODE == 1)。

Node.NodeValue 声明 Element 类型的所有节点都将返回 null。

在我的网络服务中,我创建了一个带有 XML 的字符串,即 xml="Hank"

然后,我创建 XmlDocument,

XmlDocument doc = new XmlDocument();
doc.LoadXML(string);

我知道我可以使用 CreateNode 指定节点类型。但是,当我只是通过附加字符串值来构建 xml 时,有一种方法可以将 nodeType 更改为 Text,这样 Node.nodeValue 将是“文本节点的内容”。

I have an ASP.NET web service which returns an XMLDocument. The web service is called from a Firefox extension using XMLHttpRequest.

var serviceRequest = new XMLHttpRequest();
serviecRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

I consume the result using responseXML. So far so good. But when I iterate through the XML I retrieve nodeValue - nodeValue is always null. When I check the nodeType the nodeType is type 1 (Node.ELEMENT_NODE == 1).

Node.NodeValue states all nodes of type Element will return null.

In my webservice I have created a string with the XML i.e. xml="<Root><Book><Author>Hank</Author></Book></Root>"

I then create the XmlDocument

XmlDocument doc = new XmlDocument();
doc.LoadXML(string);

I know I can specify the nodetype using using CreateNode. But when I am just building the xml by appending string values is there a way to change the nodeType to Text so Node.nodeValue will be "content of the text node".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜吻♂芭芘 2024-09-09 22:38:36

我只需要停下来查看文档。

nodeValue 是标准 DOM 属性,它返回一个 nodeValue。属性或文本等节点类型具有值。元素没有值——它们只有子节点。

因此,我只需要调用node.firstChild.nodeValue,而不是node.nodeValue。

这是因为仅包含文本的简单 XML 元素看起来应该具有其文本值,但它实际上是具有单个子节点的元素。子节点是一个文本节点,并且它是具有值的文本节点。

I just had to stop and review the documentation.

nodeValue is s standard DOM property and it returns a nodeValue. Node types such as attributes or text have a value. Elements do not have a value - they only have child nodes.

So rather than node.nodeValue I just needed to call node.firstChild.nodeValue.

That is because a simple XML element that only contains text seems like it should have a value of its text but it is actually an element that has a single child node. The child node is a text node and its the text node that has the value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文