为了账号安全,请及时绑定邮箱和手机立即绑定

将XDocument转换为XmlDocument,反之亦然

将XDocument转换为XmlDocument,反之亦然

偶然的你 2019-12-09 14:25:37
我有一个非常简单的问题。我使用XDocument生成XML文件。然后,我想将其作为XmlDocument类返回。我有一个XmlDocument变量,我需要将其转换回XDocument以附加更多节点。那么,在XDocument和XmlDocument之间转换XML 的最有效方法是什么?(不使用文件中的任何临时存储。)
查看完整描述

3 回答

?
撒科打诨

TA贡献1934条经验 获得超2个赞

对我来说,单线解决方案效果很好


XDocument y = XDocument.Parse(pXmldoc.OuterXml); // where pXmldoc is of type XMLDocument


查看完整回答
反对 回复 2019-12-09
?
qq_笑_17

TA贡献1818条经验 获得超7个赞

using System.Xml;

using System.Xml.Linq;


   #region Extention Method

    public static XElement ToXElement(this XmlElement element)

    {

        return XElement.Parse(element.OuterXml);

    }


    public static XmlElement ToXmlElement(this XElement element)

    {

        var doc = new XmlDocument();

        doc.LoadXml(element.ToString());

        return doc.DocumentElement;            

    }

    #endregion

而不是简单地使用这样的扩展来使用此扩展


System.Xml.XmlElement systemXml = (new XElement("nothing")).ToXmlElement();

System.Xml.Linq.XElement linqXml = systemXml.ToXElement();


查看完整回答
反对 回复 2019-12-09
  • 3 回答
  • 0 关注
  • 599 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信