简体   繁体   English

XDocument到XElement

[英]XDocument to XElement

How do you convert an XDocument to an XElement? 如何将XDocument转换为XElement?

I found the following by searching, but it's for converting between XDocument and XmlDocument, not XDocument and XElement. 我通过搜索找到了以下内容,但是它用于在XDocument和XmlDocument之间进行转换,而不是XDocument和XElement。

public static XElement ToXElement(this XmlElement xmlelement)
{
    return XElement.Load(xmlelement.CreateNavigator().ReadSubtree());
}

public static XmlDocument ToXmlDocument(this XDocument xdoc)
{
    var xmldoc = new XmlDocument();
    xmldoc.Load(xdoc.CreateReader());
    return xmldoc;
}

I couldn't find anything to convert an XDocument to an XElement. 我找不到任何将XDocument转换为XElement的东西。 Any help would be appreciated. 任何帮助,将不胜感激。

Other people have said it, but here's explicitly a sample to convert XDocument to XElement: 其他人已经说过了,但是这里显然是一个将XDocument转换为XElement的示例:

 XDocument doc = XDocument.Load(...);
 return doc.Root;

XDocument to XmlDocument: XDocument到XmlDocument:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xdoc.CreateReader());

XmlDocument to XDocument XmlDocument到XDocument

XDocument xDoc = XDocument.Load(new XmlNodeReader(xmlDoc));

To get the root element from the XDocument you use xDoc.Root 要从XDocument获取根元素,请使用xDoc.Root

Simple conversion from XDocument to XElement 从XDocument到XElement的简单转换

XElement cvtXDocumentToXElement(XDocument xDoc)
{
    XElement xmlOut = XElement.Parse(xDoc.ToString());
    return xmlOut;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM