Append XML string block to existing XmlDocument

前端 未结 6 1468
礼貌的吻别
礼貌的吻别 2020-12-14 15:29

I have an XmlDocument that already exists and is read from a file.

I would like to add a chunk of Xml to a node in the document. Is there a good way to create and a

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-14 16:01

    I suggest using XmlDocument.CreateDocumentFragment if you have the data in free form strings. You'll still have to use AppendChild to add the fragment to a node, but you have the freedom of building the XML in your StringBuilder.

    XmlDocument xdoc = new XmlDocument();
    xdoc.LoadXml(@"");
    
    XmlDocumentFragment xfrag = xdoc.CreateDocumentFragment();
    xfrag.InnerXml = @"";
    
    xdoc.DocumentElement.FirstChild.AppendChild(xfrag);
    

提交回复
热议问题