格式化XML:输出有缩进效果的XML字符串
1. 一般情况下使用以下代码即可将XML字符串重新格式化:
private string FormatXml(string source)
{
StringBuilder sb = new StringBuilder();
XmlTextWriter writer = null;
try
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(source);
writer = new XmlTextWriter(new StringWriter(sb));
writer.Formatting = Formatting.Indented;
doc.WriteTo(writer);
}
finally
{
if (writer != null) writer.Close();
}
 
相关文档:
同步程序案例
procedure TGetOrderThread.PostDB(webnr:WideString);
var
Err: String;
SetWebnr:WideString;
xmlDoc: IXMLDocument;
root: IXMLNode;
rowc: IXMLNode;//记录数
rows: IXMLNodeList;//主记录列表
row: IXMLNode;
drows: IXMLNodeList;// ......
ArrayCollection转成xml类型示例
下面模拟一组数据exams,将其转化为xml形式.详细代码见下:
Xml代码
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" fontSize="12" creationComplet ......
本次遇到的问题是:在上传文件时,返回的json数据被加上了<pre></pre>标签。(在普通的表单提交并不会加上<pre>标签)
利用firebug查看错误信息为:
missing } in XML expression (<pre>{success:true, msg:'成功'}</pre>)
对此问题解决如下:
对response对象设置返回类型:resp.s ......
以下是一个通过minidom模块写文件的完整示例,是在最近做的项目Walle上面用到的,这个示例的目的是生成一个如下的格式的xml,文件格式为无BOM utf-8。
生成xml文件格式:
<?xml version="1.0" encoding="utf-8"?>
<coverages>
<coverage>
  ......
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:amq="http://activemq.org/config/1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/bea ......