一些工具函数 Xml 序列化
public sealed class XmlHelper
{
public static void Serialize<T>(T obj,string fileName)
{
TextWriter writer = new StreamWriter(fileName);
try
{
XmlSerializer ser = new XmlSerializer(typeof(T));
ser.Serialize(writer, obj);
}
catch (Exception)
{
throw;
}
finally
{
if (writer != null)
{
writer.Close();
}
}
}
public static T Deserialize<T>(string fileName) where T:class
{
TextReader reader = new StreamReader(fileName);
T newObj ;
try
{
XmlSerializer mySerializer = new XmlSerializer(typeof(T));
newObj= mySerializer.Deserialize(reader) as T;
}
catch (Exception)
{
throw;
}
finally
{
if (reader != null)
{
reader.Close();
}
}
return newObj;
}
public static T Deserialize<T>(FileInfo file) where T : class
{
string fileName = file.FullName;
return Deserialize<T>(fileName);
}
}
相关文档:
XML 作为数据源的实例(TESTED)
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" preinitialize="preInit()" fontSize="12" height="500">
<mx:Script>
  ......
XML How to Program
Beginning Xml Databases
Beginning XSLT and XPath Transforming XML Documents and Data
ASP.NET 2.0 XML
XML 手册 4th Edition
XML Schema Complete Reference
......
下载 dom4j-1.6.1.jar。
1: package org.zzp.common.xml.dom4j;
2:
3: import java.io.FileWriter;
4: import java.io.IOException;
5: import org.dom4j.Document;
6: import org.dom4j.DocumentHelper;
7: import org.dom4j.Element;
8: import org.dom4j.io.OutputFormat;
9: impor ......
需要dom4j.jar文件 ,自行下载。 test.xml 1: <?xml version="1.0" encoding="gbk"?>
2:
3: <students>
4: <person sex="男" age="21">
5: <id>1</id>
6: <name>章治鹏</name>
7: <homepage&g ......
这篇文章被转载的次数最多,其实代码简陋得我自己都看不下去。只不过发表这篇文章时很多人需要这个功能。
这几天写个数据库查询分析器,要用到XML记录用户注册的数据库连接地址、端口等信息,最开始想用java ......