格式化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();
}
 
相关文档:
个人收集、整理了一些LINQ TO XML的基本方法,希望各位大虾多多指导:
/// <summary>
///Xml节点属性
/// </summary>
public class XmlAttribute
{
public XmlAttribute()
{
}
public XmlAttribute(string _key,object _value)
&nbs ......
package com.pk.xml;
import java.io.File;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Dom4j {
public static void main(String[] args) {
try {
//获得SAX解析器
SAXReader reader = new SAXReader();
//解析文件
File file = n ......
3private XmlDocument xmlDoc;
4 //load xml file
5 private void LoadXml()
6 {
......
今天才知道CMarkup可以直接解析字符串形式的XML。以前都是先存入一个文件,然后从文件中load。多做了I/O操作,效率不高。
CMarkup xml;
CString str;
xml.SetDoc(str);
tinyXml也可以直接解析XML字符串,方式如下:
// directly parsing string with tinyxml
const char* ......
<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 ......