将 XmlDocument 对象转换为 XmlNode 对象 - C#?

发布于 2024-08-23 11:20:06 字数 123 浏览 4 评论 0原文

如何在 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 技术交流群。

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

发布评论

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

评论(3

以为你会在 2024-08-30 11:20:06

XmlDocument 是一个 XmlNode,因此您可以只传递文档对象。

或者您可以发送其 DocumentElement 或从 XPath 查询返回的任何节点。

XmlDocument doc = null;
XmlNode node = doc;

XmlNode node = doc.DocumentElement;

XmlNode node = doc.SelectSingleNode("/foo/bar");

除非您需要为具有两种参数类型重载的方法消除 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.

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. If this is the case, use either of the cast or as operators.

思慕 2024-08-30 11:20:06

如果您需要将其显式引用为 XmlNode,请使用“as”:

XmlDocument doc = ...

XmlNode node = doc as XmlNode;

If you need to refer to it explicitly as an XmlNode use "as":

XmlDocument doc = ...

XmlNode node = doc as XmlNode;
护你周全 2024-08-30 11:20:06

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.

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