将XDocument转换为XmlDocument,反之亦然

我有一个非常简单的问题。我使用XDocument生成XML文件。然后,我想将其作为XmlDocument类返回。我有一个XmlDocument变量,我需要将其转换回XDocument以附加更多节点。


那么,在XDocument和XmlDocument之间转换XML 的最有效方法是什么?(不使用文件中的任何临时存储。)


偶然的你
浏览 604回答 3
3回答

撒科打诨

对我来说,单线解决方案效果很好XDocument y = XDocument.Parse(pXmldoc.OuterXml); // where pXmldoc is of type XMLDocument

qq_笑_17

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();
打开App,查看更多内容
随时随地看视频慕课网APP