将 XmlDocument 对象转换为 XmlNode 对象 - C#?
如何在 C# 中将 XmlDocument
转换为 XmlNode
?我需要将整个 XmlDocument
对象作为输入参数发送到 .NET Web 服务。
How do I convert an XmlDocument
to a XmlNode
in C#? I need to send the entire XmlDocument
object to as an input parameter to a .NET web service.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
XmlDocument 是一个 XmlNode,因此您可以只传递文档对象。
或者您可以发送其 DocumentElement 或从 XPath 查询返回的任何节点。
除非您需要为具有两种参数类型重载的方法消除 XmlNode 与 XmlDocument 的歧义,否则不需要进行强制转换或转换。如果是这种情况,请使用强制转换或
as
运算符。A XmlDocument is a XmlNode, so you can just pass the document object.
Or you could send its DocumentElement, or any Node returned from an XPath query.
No casting or converting is needed unless you need to disambiguate XmlNode from XmlDocument for a method with overloads for both parameter types. If this is the case, use either of the cast or
as
operators.如果您需要将其显式引用为 XmlNode,请使用“as”:
If you need to refer to it explicitly as an XmlNode use "as":
XmlDocument 派生自 XmlNode,但您也可以发送 XmlDocument.DocumentElement,它是 XmlElement,但最终派生自 XmlNode。您可能需要检查 XmlDocument.DocumentElement == null。
An XmlDocument is derived from XmlNode, but you could also send the XmlDocument.DocumentElement which is an XmlElement but ultimately derived from XmlNode. You might need to check in XmlDocument.DocumentElement == null.