How to remove whitespace from an XmlDocument

前端 未结 2 664
-上瘾入骨i
-上瘾入骨i 2020-12-06 13:38

I have an XML document from which I want to remove white spaces and carriage returns. How can I get the modified XML using C#.

相关标签:
2条回答
  • 2020-12-06 14:00

    To remove white spaces between the tags:

    # Regex regex = new Regex(@">\s*<");  
    # string cleanedXml = regex.Replace(dirtyXml, "><");
    

    Source and other usefull info here

    0 讨论(0)
  • 2020-12-06 14:27

    Set the preserveWhitespace flag to false:

    XmlDocument doc = new XmlDocument();
    doc.PreserveWhitespace = false;
    doc.Load("foo.xml");
    // doc.InnerXml contains no spaces or returns
    
    0 讨论(0)
提交回复
热议问题