[英]Convert XmlDocument object into an XmlNode object - C#?
How do I convert an XmlDocument
to a XmlNode
in C#? 如何在C#中将
XmlDocument
转换为XmlNode
? I need to send the entire XmlDocument
object to as an input parameter to a .NET web service. 我需要将整个
XmlDocument
对象作为输入参数发送到.NET Web服务。
A XmlDocument is a XmlNode, so you can just pass the document object. XmlDocument是一个XmlNode,因此您只需传递文档对象即可。
Or you could send its DocumentElement, or any Node returned from an XPath query. 或者您可以发送其DocumentElement或从XPath查询返回的任何节点。
XmlDocument doc = null;
XmlNode node = doc;
XmlNode node = doc.DocumentElement;
XmlNode node = doc.SelectSingleNode("/foo/bar");
No casting or converting is needed unless you need to disambiguate XmlNode from XmlDocument for a method with overloads for both parameter types. 除非您需要从XmlDocument中消除XmlNode的歧义,否则不需要进行转换或转换,以获取两种参数类型都有重载的方法。 If this is the case, use either of the cast or
as
operators. 如果是这种情况,使用的无论是铸造或
as
运营商。
If you need to refer to it explicitly as an XmlNode use "as": 如果需要将其明确引用为XmlNode,请使用“as”:
XmlDocument doc = ...
XmlNode node = doc as XmlNode;
An XmlDocument is derived from XmlNode, but you could also send the XmlDocument.DocumentElement which is an XmlElement but ultimately derived from XmlNode. XmlDocument派生自XmlNode,但您也可以发送XmlDocument.DocumentElement,它是一个XmlElement,但最终派生自XmlNode。 You might need to check in XmlDocument.DocumentElement == null.
您可能需要签入XmlDocument.DocumentElement == null。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.