How do I preserve all XML formatting with XDocument?

后端 未结 2 2143
盖世英雄少女心
盖世英雄少女心 2021-02-14 17:53

I\'m trying to read in an XML configuration file, do a few tweaks (finding and removing or adding an element) and save it again. I want this edit to be as non-intrusive as possi

2条回答
  •  甜味超标
    2021-02-14 18:19

    The LINQ to XML object model does not store whether an element parsed is marked up as or so when saving back such information is lost. If you want to ensure a certain format then you could extend an XmlWriter implementation and override its http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.writeendelement.aspx but that way you would also not preserve the input format, rather you would then write out any empty elements as or whatever format you implement in your method.

    There are other changes that can happen, for instance when loading the file

    
      
        Example
      
      
        

    Example

    and saving it back the result is

    
      
        Example
      
      
        Example
      
    
    

    so don't expect markup details to be preserved when loading/saving with XDocument/XElement.

提交回复
热议问题