估計以下同樣使用XDocument的人比較多,畢竟也是微軟推薦使用的。html
string FormatXml(string Xml) { try { XDocument doc = XDocument.Parse(Xml); return doc.ToString(); } catch (Exception) { return Xml; } }
當出現以下文檔(默認命名空間,前綴命名空間都定義)時,以上方法返回的值格式變了:ios
<?xml version="1.0"?> <?mso-application progid="Excel.Sheet"?> <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40"> <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office"> <Created>2015-06-05T18:19:34Z</Created> <LastSaved>2015-06-05T18:19:39Z</LastSaved> <Version>16.00</Version> </DocumentProperties> <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office"> <AllowPNG/> <RemovePersonalInformation/> </OfficeDocumentSettings> <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel"> <WindowHeight>12645</WindowHeight> <WindowWidth>22260</WindowWidth> <WindowTopX>1170</WindowTopX> <WindowTopY>0</WindowTopY> <ProtectStructure>False</ProtectStructure> <ProtectWindows>False</ProtectWindows> </ExcelWorkbook> <Styles> <Style ss:ID="Default" ss:Name="Normal"> <Alignment ss:Vertical="Bottom"/> <Borders/> <Font ss:FontName="遊ゴシック" x:CharSet="128" x:Family="Modern" ss:Size="11" ss:Color="#000000"/> <Interior/> <NumberFormat/> <Protection/> </Style> </Styles> <Worksheet ss:Name="Sheet1"> <Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="1" x:FullColumns="1" x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="18.75"> </Table> <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel"> <PageSetup> <Header x:Margin="0.3"/> <Footer x:Margin="0.3"/> <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/> </PageSetup> <Print> <ValidPrinterInfo/> <PaperSizeIndex>9</PaperSizeIndex> <HorizontalResolution>600</HorizontalResolution> <VerticalResolution>600</VerticalResolution> </Print> <Selected/> <ProtectObjects>False</ProtectObjects> <ProtectScenarios>False</ProtectScenarios> </WorksheetOptions> </Worksheet> </Workbook>
XDocument整形以後,前綴顯示自動加上了:app
<?xml version="1.0" encoding="utf-8"?> <?mso-application progid="Excel.Sheet"?> <ss:Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40"> <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office"> <Created>2015-06-05T18:19:34Z</Created> <LastSaved>2015-06-05T18:19:39Z</LastSaved> <Version>16.00</Version> </DocumentProperties> <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office"> <AllowPNG /> <RemovePersonalInformation /> </OfficeDocumentSettings> <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel"> <WindowHeight>12645</WindowHeight> <WindowWidth>22260</WindowWidth> <WindowTopX>1170</WindowTopX> <WindowTopY>0</WindowTopY> <ProtectStructure>False</ProtectStructure> <ProtectWindows>False</ProtectWindows> </ExcelWorkbook> <ss:Styles> <ss:Style ss:ID="Default" ss:Name="Normal"> <ss:Alignment ss:Vertical="Bottom" /> <ss:Borders /> <ss:Font ss:FontName="遊ゴシック" x:CharSet="128" x:Family="Modern" ss:Size="11" ss:Color="#000000" /> <ss:Interior /> <ss:NumberFormat /> <ss:Protection /> </ss:Style> </ss:Styles> <ss:Worksheet ss:Name="Sheet1"> <ss:Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="1" x:FullColumns="1" x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="18.75"></ss:Table> <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel"> <PageSetup> <Header x:Margin="0.3" /> <Footer x:Margin="0.3" /> <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75" /> </PageSetup> <Print> <ValidPrinterInfo /> <PaperSizeIndex>9</PaperSizeIndex> <HorizontalResolution>600</HorizontalResolution> <VerticalResolution>600</VerticalResolution> </Print> <Selected /> <ProtectObjects>False</ProtectObjects> <ProtectScenarios>False</ProtectScenarios> </WorksheetOptions> </ss:Worksheet> </ss:Workbook>
解決辦法:使用XmlDocumentui
static string FormatXml(string xml) { try { var doc = new XmlDocument(); doc.LoadXml(xml); StringBuilder output = new StringBuilder(); XmlWriterSettings settings = new XmlWriterSettings { Indent = true, Async = true }; using (XmlWriter writer = XmlWriter.Create(output, settings)) { doc.Save(writer); } return output.ToString(); } catch (Exception) { return xml; } }
整形以後保持原樣:spa
<?xml version="1.0" encoding="utf-16"?> <?mso-application progid="Excel.Sheet"?> <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40"> <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office"> <Created>2015-06-05T18:19:34Z</Created> <LastSaved>2015-06-05T18:19:39Z</LastSaved> <Version>16.00</Version> </DocumentProperties> <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office"> <AllowPNG /> <RemovePersonalInformation /> </OfficeDocumentSettings> <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel"> <WindowHeight>12645</WindowHeight> <WindowWidth>22260</WindowWidth> <WindowTopX>1170</WindowTopX> <WindowTopY>0</WindowTopY> <ProtectStructure>False</ProtectStructure> <ProtectWindows>False</ProtectWindows> </ExcelWorkbook> <Styles> <Style ss:ID="Default" ss:Name="Normal"> <Alignment ss:Vertical="Bottom" /> <Borders /> <Font ss:FontName="遊ゴシック" x:CharSet="128" x:Family="Modern" ss:Size="11" ss:Color="#000000" /> <Interior /> <NumberFormat /> <Protection /> </Style> </Styles> <Worksheet ss:Name="Sheet1"> <Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="1" x:FullColumns="1" x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="18.75"></Table> <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel"> <PageSetup> <Header x:Margin="0.3" /> <Footer x:Margin="0.3" /> <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75" /> </PageSetup> <Print> <ValidPrinterInfo /> <PaperSizeIndex>9</PaperSizeIndex> <HorizontalResolution>600</HorizontalResolution> <VerticalResolution>600</VerticalResolution> </Print> <Selected /> <ProtectObjects>False</ProtectObjects> <ProtectScenarios>False</ProtectScenarios> </WorksheetOptions> </Worksheet> </Workbook>
標籤的值中若是包含\r\n換行符,XDocument以及XmlDocument讀取以後會默認被轉換爲\n,若是就這樣保存會少了\r。excel
並且Excel另存爲的xml格式文件中的改行符爲【 】,很難直接復原。code
若是不在乎xml格式的話能夠經過以下方法解決:orm
static string FormatXml(string xml) { try { var doc = new XmlDocument(); doc.LoadXml(xml); StringBuilder output = new StringBuilder(); XmlWriterSettings settings = new XmlWriterSettings { NewLineChars = "\r\n", NewLineOnAttributes = true, NewLineHandling = NewLineHandling.Replace, CheckCharacters = false, Indent = false, Async = true }; using (XmlWriter writer = XmlWriter.Create(output, settings)) { doc.Save(writer); } return output.ToString(); } catch (Exception) { return xml; } }