Removing preprocessing instructions from an XML in c#

后端 未结 2 838
暖寄归人
暖寄归人 2021-01-26 04:33

How can i remove preprocessing instructions from an XML in a greener way? suppose that I have this xml in a string variable (which is a property of a class),I wante

相关标签:
2条回答
  • 2021-01-26 05:07

    If you can load it in an XmlDocument: FirstChild returns the <?xml version="1.0" encoding="utf-8" ?> node, and NextSibling returns the rest.

    XmlDocument doc = new XmlDocument();
    doc.Load(path);
    XmlNode node = doc.FirstChild.NextSibling;
    

    Edit: Your xml looks a lot like the example on msdn for XPathNavigator.Select. Have you tried using that?

    Edit2: You can get the name of the top level element using:

    string topLevelNode = doc.DocumentElement.Name;
    
    0 讨论(0)
  • 2021-01-26 05:22

    Try the code below. It will load the text to XmlDocument object first

    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(xmlAsText);
    

    Then you can get the OutherXml using below:

    xmlDoc.DocumentElement.OuterXml;
    
    0 讨论(0)
提交回复
热议问题