开发者

ASP.NET Web Service returning XML result and nodevalue is always null

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".


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜