XML整形以及改行字符串输出
XML整形
估计如下一样使用XDocument的人比较多,毕竟也是微软推荐使用的。
string FormatXml(string Xml)
{
try
{
XDocument doc = XDocument.Parse(Xml);
return doc.ToString();
}
catch (Exception)
{
return Xml;
}
}
当出现如下文档(默认命名空间,前缀命名空间都定义)时,以上方法返回的值格式变了:
<?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整形之后,前缀显示自动加上了:
<?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>
解决办法:使用XmlDocument
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;
}
}
整形之后保持原样:
<?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>
改行(CR+LF)问题
标签的值中如果包含\r\n换行符,XDocument以及XmlDocument读取之后会默认被转换为\n,如果就这样保存会少了\r。
而且Excel另存为的xml格式文件中的改行符为【 】,很难直接复原。
如果不在意xml格式的话可以通过如下方法解决:
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;
}
}
XML整形以及改行字符串输出的更多相关文章
- 将filenames里的每个字符串输出到out文件对象中注意行首的缩进
在Linux上用强大的shell脚本应该也可以完成,可是使用Windows的朋友呢?其实象这样一个简单任务用Python这个强大脚本语言只要几条语句就可以搞定了.个大家知道,要完成这样一个任务根本不用 ...
- Winform: use the WebBrowser to display XML with xslt, xml, xslt 转 html 字符串
原文:Winform: use the WebBrowser to display XML with xslt, xml, xslt 转 html 字符串 声明xml字符串: string xml = ...
- 【转】xml节点解析成字符串的方法
网址:http://blog.csdn.net/shanzhizi/article/details/8817532 ZC: 这是 libxml2的 之前汇总了一篇关于xml文档与字符串转换的文章,文章 ...
- 批处理将字符串输出到Windows剪贴板
批处理将字符串输出到Windows剪贴板 2016-06-30 23:29 339人阅读 评论(0) 收藏 举报 版权声明:作者:N3verL4nd 出处:http://blog.csdn.net/x ...
- 8 C#中的字符串输出
我们在前面已经用Console.WriteLine("*********")往dos窗口中输出过字符串.我们还定义过字符串的变量. string words ="我喜欢D ...
- 《c程序设计语言》读书笔记--大于8 的字符串输出
#include <stdio.h> #define MAXLINE 100 #define MAX 8 int getline(char line[],int maxline); voi ...
- implode 多维数组转一维数组并字符串输出
//多维数组返回一维数组,拼接字符串输出 public function r_implode( $glue, $pieces ) { foreach( $pieces as $r_pieces ) { ...
- Python学习笔记 (2) :字符串输出、操作、格式化和日期、时间格式化
一.字符串输出及运算 1.常用输出格式及方法 ')#单引号 ")#双引号 """)#三个引号 1234567890 1234567890 1234567890 ...
- repr方法字符串输出实例对象的值
#coding=utf-8 #repr方法字符串输出实例对象的值 class CountFromBy(object): def __init__(self, val=0, incr=1): self. ...
随机推荐
- 异常值检测方法(Z-score,DBSCAN,孤立森林)
机器学习_深度学习_入门经典(博主永久免费教学视频系列) https://study.163.com/course/courseMain.htm?courseId=1006390023&sh ...
- Mac下GoogleChromeHelper占用内存过高 的一个排查过程记录
测试需要在Mac上装了个虚拟机,结果忘记限制资源了,直接崩溃重启过一次. 后面限制了一下资源,发现内存占用率还是特别高,其中最高的居然是Chrome相关的一个东西.这让我8G内存该如何是好. 于是查了 ...
- base64加密后无法解密
记录一个问题: 使用java,或者命令行 base64 命令加密图片文件成加密数据后无法还原成图片 深入:使用java base64工具(sun base64或bouncycastle)加密的数据替换 ...
- MySQL 使用 ON UPDATE CURRENT_TIMESTAMP 自动更新 timestamp (转)
原文地址: https://blog.csdn.net/heatdeath/article/details/79833492 `create_time` timestamp not null defa ...
- [ ceph ] 基本介绍及硬件配置
1. Ceph简介 所有的 Ceph 存储集群的部署都始于一个个 Ceph节点.网络和 Ceph存储集群.Ceph 存储集群至少需要一个 Ceph Monitor.一个 Manager和一个Ceph ...
- [Python] 项目的配置覆盖与合并
参考来源: https://www.liaoxuefeng.com/wiki/1016959663602400/1018490750237280 代码稍微修改了一下 import os import ...
- 【maven学习】settings.xml文件详解
环境 apache-maven-3.6.1 jdk 1.8 eclipse 4.7 Settings.xml是设置maven参数的配置文件,包含类似本地仓储位置.修改远程仓储服务器.认证信息等配置.p ...
- [Linux]Linux下samba创建共享文件
1. 安装samba服务 yum install -y samba 2. 创建需要共享的目录 在目录/home/xxxx/share xxx为用户名 mkdir share 修改该目录权限(上层文件夹 ...
- mysql 连接远程阿里云数据库
一.修改mysql 数据库的远程访问权限 use mysql; SELECT HOST,user,PASSWORD FROM USER; -- 查询用户信息 UPDATE USER SET HOST= ...
- python数据分析2之numpy
源代码 # -*- coding: utf-8 -*- """ Spyder Editor This is a temporary script file. " ...