将xmlstring转换为XmlNode

use*_*834 7 .net c# xml

我有一个像这样的xml字符串

string stxml="<Status>Success</Status>";
Run Code Online (Sandbox Code Playgroud)

我还创建了一个xml文档

  XmlDocument doc = new XmlDocument();
  XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
  doc.AppendChild(docNode);
  XmlNode rootNode = doc.CreateElement("StatusList");
  doc.AppendChild(rootNode);
Run Code Online (Sandbox Code Playgroud)

我需要这样的输出.

  <StatusList>
  <Status>Success</Status>
  </StatusList>
Run Code Online (Sandbox Code Playgroud)

如何实现这一点.如果我们使用innerhtml,它会插入.但我想将xml字符串作为xmlnode本身插入

das*_*ash 17

实现目标的一种非常简单的方法是使用经常被忽略的XmlDocumentFragment类:

  XmlDocument doc = new XmlDocument();
  XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
  doc.AppendChild(docNode);
  XmlNode rootNode = doc.CreateElement("StatusList");
  doc.AppendChild(rootNode);

  //Create a document fragment and load the xml into it
  XmlDocumentFragment fragment = doc.CreateDocumentFragment();
  fragment.InnerXml = stxml;
  rootNode.AppendChild(fragment);
Run Code Online (Sandbox Code Playgroud)