.Net中处理XML的常用API
.Net对XML提供了丰富的API,但大多数都在日常开发中涉及不到。先总结下一些使用频繁的API与大家分享。
测试数据
bookstore.xml
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="bookstore.xslt"?>
<bookstore>
<books>
<book id="1">
<name>十万个为什么</name>
<price>30</price>
<author>为什么先生</author>
<publisher>火星出版社</publisher>
<publisher-time>2012年12月12日</publisher-time>
</book>
<book id="2">
<name>客官不要嘛</name>
<price>10</price>
<author>客官</author>
<publisher>马虎出版社</publisher>
<publisher-time>2011年8月8日</publisher-time>
</book>
<book id="3">
<name>vb编程</name>
<price>49</price>
<author>逍遥哥</author>
<publisher>逍遥出版社</publisher>
<publisher-time>2010年5月8日</publisher-time>
</book>
<book id="4">
<name>vf编程</name>
<price>40</price>
<author>逍遥妹</author>
<publisher>清华出版社</publisher>
<publisher-time>2003年4月2日</publisher-time>
</book>
<book id="5">
<name>c#编程入门</name>
<price>50</price>
<author>c#高手</author>
<publisher>清华出版社</publisher>
<publisher-time>2002年2月3日</publisher-time>
</book>
<book id="6">
<name>vb.net编程入门</name>
<price>69</price>
<author>vb哥</author>
<publisher>wrox</publisher>
<publisher-time>2001年1月30日</publisher-time>
</book>
<book id="7">
<name>c#电子商务编程</name>
<price>90</price>
<author>电商哥</author>
<publisher>wrox</publisher>
<publisher-time>2001年1月30日</publisher-time>
</book>
<book id="8">
<name>sql server 2000编程</name>
<price>45</price>
<author>sql哥</author>
<publisher>GG出版社</publisher>
<publisher-time>2005年5月30日</publisher-time>
</book>
<book id="9">
<name>oracle 9i 编程</name>
<price>88</price>
<author>oracle哥</author>
<publisher>wrox</publisher>
<publisher-time>2006年12月30日</publisher-time>
</book>
<book id="10">
<name>sql server 2008入门经典</name>
<price>70</price>
<author>sql server08</author>
<publisher>wrox出版社</publisher>
<publisher-time>2001年1月30日</publisher-time>
</book>
</books>
<media>
<cd id="1">
<name>爱你不容易</name>
<author>刘德华</author>
<price>99</price>
<publish-time>2006年1月1日</publish-time>
</cd>
</media>
</bookstore>
books.xsd
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Book"
targetNamespace="http://tempuri.org/Book.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/Book.xsd"
xmlns:mstns="http://tempuri.org/Book.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xs:element name="Books">
<xs:complexType>
<xs:sequence>
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="Name"></xs:element>
<xs:element name="ISBN"></xs:element>
<xs:element name="Author"></xs:element>
<xs:element name="Publisher"></xs:element>
<xs:element name="Price"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
demobook.xml
<?xml version="1.0" encoding="utf-8" ?>
<Books xmlns="http://tempuri.org/Book.xsd">
<book>
<Name>c#入门经典</Name>
<ISBN>P1234567890</ISBN>
<Author>王小帅</Author>
<Publisher>清华出版社</Publisher>
<Price>59</Price>
</book>
<book>
<Name>c#入门经典</Name>
<ISBN>P1234567890</ISBN>
<Author>王小帅</Author>
<Publisher>清华出版社</Publisher>
<Price>59</Price>
</book>
</Books>
bookstore.xslt
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:wrox="http://www.wrox.com"
exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<html>
<head>
<title>bookstore</title>
</head>
<body>
<h1>图书</h1>
<table width="60%" border="1" cellpadding="5" cellspacing="0" bordercolor="#000000">
<tr>
<th>书名</th>
<th>价格</th>
<th>作者</th>
<th>出版社</th>
</tr>
<xsl:apply-templates select="/bookstore/books"></xsl:apply-templates>
</table>
<h1>音像制品</h1>
<ul>
<xsl:apply-templates select="/bookstore/media"></xsl:apply-templates>
</ul>
</body>
</html>
</xsl:template>
<xsl:template match="/bookstore/books">
<xsl:for-each select="./book">
<tr>
<td>
<xsl:value-of select="./name"/>
</td>
<td>
<xsl:value-of select="./price"/>
</td>
<td>
<xsl:value-of select="./author"/>
</td>
<td>
<xsl:value-of select="./publisher"/>
</td>
</tr>
</xsl:for-each>
</xsl:template>
<xsl:template match="/bookstore/media">
<xsl:for-each select="cd">
<li>
<xsl:value-of select="./name"/>
</li>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Book.cs(支持xml序列化的类)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
namespace chapter33
{
[XmlRoot()]
public class book
{
public book()
{
}
[XmlAttribute(AttributeName="id")]
public int Id { get; set; }
[XmlElement(ElementName="name")]
public string Name { get; set; }
[XmlElement(ElementName="price")]
public string Price { get; set; }
[XmlElement(ElementName="author")]
public string Author { get; set; }
[XmlElement(ElementName="publisher")]
public string Publisher { get; set; }
[XmlElement(ElementName="publishertime")]
public string PublisherTime { get; set; }
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("id:{0}", Id);
sb.AppendFormat("name:{0}", Name);
sb.AppendFormat("price:{0}", Price);
sb.AppendFormat("author:{0}", Author);
sb.AppendFormat("publisher:{0}", Publisher);
sb.AppendFormat("publishertime:{0}", PublisherTime);
return sb.ToString();
}
}
}
XmlReader
该类提供读取XML的能力,它是个单向读取数据的游标。
DEMO
//XmlReader的使用,2层xmlread循环读取。打印出所有book节点下的name节点中保存的内容
XmlReader reader = XmlReader.Create("d:\\bookstore.xml");
int tmpIndex = 1;
while (reader.Read())
{
//Console.WriteLine(reader.Value);
if (reader.NodeType == XmlNodeType.Element && reader.Name.ToLower() == "book")
{
tmpIndex = int.Parse(reader.GetAttribute("id"));
using (var tmpReader = reader.ReadSubtree())
{
while (tmpReader.Read())
{
if (tmpReader.NodeType == XmlNodeType.Element && tmpReader.Name.ToLower() == "name")
{
Console.WriteLine("["+tmpIndex.ToString()+"]"+tmpReader.ReadString());
}
}
}
}
}
reader.Close();
执行结果:
[1]十万个为什么 |
XmlReader和XSD
XSD是XML的架构文件,用来验证XML是否符合规定。XmlReader在读取XML文件的时候可以通过关联XSD文件来保证所读取的内容都是合法的。
DEMO
//结合Xsd的XmlReader
XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add("http://tempuri.org/Book.xsd", "d:\\Book.xsd");
settings.ValidationEventHandler += (s, e) => { Console.WriteLine("ValidationEvent!"); };
XmlReader reader2=XmlReader.Create("d:\\DemoBook.xml",settings);
while(reader2.Read())
{
Console.WriteLine(reader2.ReadString());
}
reader2.Close();
public static bool IsValidXml2(string xsdContent,string xmlContent)
{
MemoryStream xsdStream = new MemoryStream(Encoding.ASCII.GetBytes(xsdContent));
MemoryStream xmlStream=new MemoryStream(Encoding.ASCII.GetBytes(xmlContent));
bool isOk = true;
using (XmlReader schemaReader = XmlReader.Create(xsdStream))
{
XmlSchema schema = XmlSchema.Read(schemaReader, null);
XDocument xdoc = XDocument.Load(xmlStream);
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add(schema);
try
{
xdoc.Validate(schemas, null);
}
catch (XmlSchemaValidationException)
{
isOk = false;
}
}
return isOk;
}
public static bool IsValidXml2(string xsdContent,string xmlContent)
{
MemoryStream xsdStream = new MemoryStream(Encoding.ASCII.GetBytes(xsdContent));
MemoryStream xmlStream=new MemoryStream(Encoding.ASCII.GetBytes(xmlContent));
bool isOk = true;
using (XmlReader schemaReader = XmlReader.Create(xsdStream))
{
XmlSchema schema = XmlSchema.Read(schemaReader, null);
XDocument xdoc = XDocument.Load(xmlStream);
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add(schema);
try
{
xdoc.Validate(schemas, null);
}
catch (XmlSchemaValidationException)
{
isOk = false;
}
}
return isOk;
}
执行结果
P1234567890
|
XmlWriter
DEMO
//XmlWriter
XmlWriterSettings settings=new XmlWriterSettings();
settings.Encoding=Encoding.UTF8;
settings.Indent=true;
XmlWriter writer = XmlWriter.Create("d:\\tmpWriter.xml",settings);
writer.WriteStartDocument();
writer.WriteStartElement("Books");
writer.WriteStartElement("Book");
writer.WriteStartAttribute("id");
writer.WriteValue(1);
writer.WriteEndAttribute();
writer.WriteStartElement("Name");
writer.WriteValue("十万个为什么");
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteStartElement("Book");
writer.WriteStartAttribute("id");
writer.WriteValue(2);
writer.WriteEndAttribute();
writer.WriteStartElement("Name");
writer.WriteValue("客官不可以");
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteStartElement("Book");
writer.WriteStartAttribute("id");
writer.WriteValue(3);
writer.WriteEndAttribute();
writer.WriteStartElement("Name");
writer.WriteValue("心朝大海");
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteStartElement("Book");
writer.WriteStartAttribute("id");
writer.WriteValue(4);
writer.WriteEndAttribute();
writer.WriteStartElement("Name");
writer.WriteValue("春暖花开");
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
执行结果
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="bookstore.xslt"?>
<bookstore>
<books>
<book id="1">
<name>十万个为什么</name>
<price>30</price>
<author>为什么先生</author>
<publisher>火星出版社</publisher>
<publisher-time>2012年12月12日</publisher-time>
</book>
<book id="2">
<name>客官不要嘛</name>
<price>10</price>
<author>客官</author>
<publisher>马虎出版社</publisher>
<publisher-time>2011年8月8日</publisher-time>
</book>
<book id="3">
<name>vb编程</name>
<price>49</price>
<author>逍遥哥</author>
<publisher>逍遥出版社</publisher>
<publisher-time>2010年5月8日</publisher-time>
</book>
<book id="4">
<name>vf编程</name>
<price>40</price>
<author>逍遥妹</author>
<publisher>清华出版社</publisher>
<publisher-time>2003年4月2日</publisher-time>
</book>
<book id="5">
<name>c#编程入门</name>
<price>50</price>
<author>c#高手</author>
<publisher>清华出版社</publisher>
<publisher-time>2002年2月3日</publisher-time>
</book>
<book id="6">
<name>vb.net编程入门</name>
<price>69</price>
<author>vb哥</author>
<publisher>wrox</publisher>
<publisher-time>2001年1月30日</publisher-time>
</book>
<book id="7">
<name>c#电子商务编程</name>
<price>90</price>
<author>电商哥</author>
<publisher>wrox</publisher>
<publisher-time>2001年1月30日</publisher-time>
</book>
<book id="8">
<name>sql server 2000编程</name>
<price>45</price>
<author>sql哥</author>
<publisher>GG出版社</publisher>
<publisher-time>2005年5月30日</publisher-time>
</book>
<book id="9">
<name>oracle 9i 编程</name>
<price>88</price>
<author>oracle哥</author>
<publisher>wrox</publisher>
<publisher-time>2006年12月30日</publisher-time>
</book>
<book id="10">
<name>sql server 2008入门经典</name>
<price>70</price>
<author>sql server08</author>
<publisher>wrox出版社</publisher>
<publisher-time>2001年1月30日</publisher-time>
</book>
</books>
<media>
<cd id="1">
<name>爱你不容易</name>
<author>刘德华</author>
<price>99</price>
<publish-time>2006年1月1日</publish-time>
</cd>
</media>
</bookstore>
XML DOM
DEMO
XmlDocument doc = new XmlDocument();
doc.Load("d:\\bookstore.xml");
XmlNodeList nodes = doc.SelectNodes("//name");//xpath表达式
//foreach (XmlNode tmp in nodes)
//{
// Console.WriteLine(tmp.InnerText);
//}
XmlElement node = doc.CreateElement("book");
var id = doc.CreateAttribute("id");
id.Value = "88";
node.Attributes.Append(id);
XmlElement name = doc.CreateElement("name");
XmlElement price=doc.CreateElement("price");
XmlElement author=doc.CreateElement("author");
XmlElement publisher=doc.CreateElement("publisher");
XmlElement publishertime=doc.CreateElement("publisher-time");
name.InnerText = "酱油秘方";
price.InnerText = "29.5";
author.InnerText = "酱油男";
publisher.InnerText = "酱油铺子";
publishertime.InnerText = "2011年10月2日";
node.AppendChild(name);
node.AppendChild(price);
node.AppendChild(author);
node.AppendChild(publisher);
node.AppendChild(publishertime);
XmlNode nodes2 = doc.SelectSingleNode("//books");
nodes2.AppendChild(node);
doc.Save("d:\\bookstore2.xml");
执行结果
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="bookstore.xslt"?>
<bookstore>
<books>
<book id="1">
<name>十万个为什么</name>
<price>30</price>
<author>为什么先生</author>
<publisher>火星出版社</publisher>
<publisher-time>2012年12月12日</publisher-time>
</book>
<book id="2">
<name>客官不要嘛</name>
<price>10</price>
<author>客官</author>
<publisher>马虎出版社</publisher>
<publisher-time>2011年8月8日</publisher-time>
</book>
<book id="3">
<name>vb编程</name>
<price>49</price>
<author>逍遥哥</author>
<publisher>逍遥出版社</publisher>
<publisher-time>2010年5月8日</publisher-time>
</book>
<book id="4">
<name>vf编程</name>
<price>40</price>
<author>逍遥妹</author>
<publisher>清华出版社</publisher>
<publisher-time>2003年4月2日</publisher-time>
</book>
<book id="5">
<name>c#编程入门</name>
<price>50</price>
<author>c#高手</author>
<publisher>清华出版社</publisher>
<publisher-time>2002年2月3日</publisher-time>
</book>
<book id="6">
<name>vb.net编程入门</name>
<price>69</price>
<author>vb哥</author>
<publisher>wrox</publisher>
<publisher-time>2001年1月30日</publisher-time>
</book>
<book id="7">
<name>c#电子商务编程</name>
<price>90</price>
<author>电商哥</author>
<publisher>wrox</publisher>
<publisher-time>2001年1月30日</publisher-time>
</book>
<book id="8">
<name>sql server 2000编程</name>
<price>45</price>
<author>sql哥</author>
<publisher>GG出版社</publisher>
<publisher-time>2005年5月30日</publisher-time>
</book>
<book id="9">
<name>oracle 9i 编程</name>
<price>88</price>
<author>oracle哥</author>
<publisher>wrox</publisher>
<publisher-time>2006年12月30日</publisher-time>
</book>
<book id="10">
<name>sql server 2008入门经典</name>
<price>70</price>
<author>sql server08</author>
<publisher>wrox出版社</publisher>
<publisher-time>2001年1月30日</publisher-time>
</book>
<book id="88">
<name>酱油秘方</name>
<price>29.5</price>
<author>酱油男</author>
<publisher>酱油铺子</publisher>
<publisher-time>2011年10月2日</publisher-time>
</book>
</books>
<media>
<cd id="1">
<name>爱你不容易</name>
<author>刘德华</author>
<price>99</price>
<publish-time>2006年1月1日</publish-time>
</cd>
</media>
</bookstore>
XPathDocument,XpathNavigator,XPathNodeIterator
DEMO
XPathDocument doc = new XPathDocument("d:\\bookstore.xml");
XPathNavigator nav = ((IXPathNavigable)doc).CreateNavigator();
XPathNodeIterator iter = nav.Select("//book[@id=4]");
while (iter.MoveNext())
{
var nav2 = iter.Current.Select("name");
while (nav2.MoveNext())
{
Console.WriteLine(nav2.Current.Value);
}
}
执行结果
vf编程 |
Xslt
Xslt是Xml中的重要级技术,Xslt往往能轻易的解决一些复杂的问题。
DEMO
XslCompiledTransform xsl = new XslCompiledTransform();
xsl.Load("d:\\bookstore.xslt");
xsl.Transform("d:\\bookstore.xml", "d:\\bookstore.html");
执行结果
DataSet与XML
DataSet可以导出问XML文件,XML文件也可以实例化为DataSet。
DEMO
SqlConnection conn = new SqlConnection("data source=192.168.6.4;initial catalog=chapter30;uid=sa;pwd=123456;");
SqlCommand cmd = new SqlCommand("select * from student",conn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
try
{
conn.Open();
adapter.Fill(ds, "Student");
}
catch
{
}
finally
{
conn.Close();
}
ds.WriteXml("d:\\Student.xml", XmlWriteMode.WriteSchema);
ds.WriteXml("d:\\Student2.xml", XmlWriteMode.DiffGram);
ds.WriteXml("d:\\Student3.xml", XmlWriteMode.IgnoreSchema);
执行结果
student.xml
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Student">
<xs:complexType>
<xs:sequence>
<xs:element name="Id" type="xs:int" minOccurs="0" />
<xs:element name="Name" type="xs:string" minOccurs="0" />
<xs:element name="Age" type="xs:string" minOccurs="0" />
<xs:element name="Grade" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<Student>
<Id>1</Id>
<Name>jim </Name>
<Age>11 </Age>
<Grade>5 </Grade>
</Student>
<Student>
<Id>2</Id>
<Name>lucy </Name>
<Age>12 </Age>
<Grade>6 </Grade>
</Student>
<Student>
<Id>3</Id>
<Name>ben </Name>
<Age>8 </Age>
<Grade>3 </Grade>
</Student>
<Student>
<Id>4</Id>
<Name>clark </Name>
<Age>5 </Age>
<Grade>2 </Grade>
</Student>
<Student>
<Id>5</Id>
<Name>bob </Name>
<Age>12 </Age>
<Grade>6 </Grade>
</Student>
<Student>
<Id>6</Id>
<Name>bomb </Name>
<Age>11 </Age>
<Grade>8 </Grade>
</Student>
<Student>
<Id>10</Id>
<Name>熊宸 </Name>
<Age>25 </Age>
<Grade>硕士 </Grade>
</Student>
<Student>
<Id>11</Id>
<Name>灵月如 </Name>
<Age>16 </Age>
<Grade>高中 </Grade>
</Student>
<Student>
<Id>13</Id>
<Name>着 </Name>
<Age>1 </Age>
<Grade>3 </Grade>
</Student>
<Student>
<Id>14</Id>
<Name>55 </Name>
<Age>55 </Age>
<Grade>55 </Grade>
</Student>
<Student>
<Id>16</Id>
<Name>superbomb </Name>
<Age>12 </Age>
<Grade>11 </Grade>
</Student>
<Student>
<Id>22</Id>
<Name>Chrimas </Name>
<Age>22 </Age>
<Grade>10 </Grade>
</Student>
<Student>
<Id>555</Id>
<Name>wcf </Name>
<Age>1 </Age>
<Grade>10 </Grade>
</Student>
<Student>
<Id>666</Id>
<Name>superbomb </Name>
<Age>1 </Age>
<Grade>999 </Grade>
</Student>
<Student>
<Id>1234</Id>
<Name>1 </Name>
<Age>2 </Age>
<Grade>3 </Grade>
</Student>
<Student>
<Id>9876</Id>
<Name>superman </Name>
<Age>10 </Age>
<Grade>9999 </Grade>
</Student>
</NewDataSet>
student2.xml
<?xml version="1.0" standalone="yes"?>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<NewDataSet>
<Student diffgr:id="Student1" msdata:rowOrder="0">
<Id>1</Id>
<Name>jim </Name>
<Age>11 </Age>
<Grade>5 </Grade>
</Student>
<Student diffgr:id="Student2" msdata:rowOrder="1">
<Id>2</Id>
<Name>lucy </Name>
<Age>12 </Age>
<Grade>6 </Grade>
</Student>
<Student diffgr:id="Student3" msdata:rowOrder="2">
<Id>3</Id>
<Name>ben </Name>
<Age>8 </Age>
<Grade>3 </Grade>
</Student>
<Student diffgr:id="Student4" msdata:rowOrder="3">
<Id>4</Id>
<Name>clark </Name>
<Age>5 </Age>
<Grade>2 </Grade>
</Student>
<Student diffgr:id="Student5" msdata:rowOrder="4">
<Id>5</Id>
<Name>bob </Name>
<Age>12 </Age>
<Grade>6 </Grade>
</Student>
<Student diffgr:id="Student6" msdata:rowOrder="5">
<Id>6</Id>
<Name>bomb </Name>
<Age>11 </Age>
<Grade>8 </Grade>
</Student>
<Student diffgr:id="Student7" msdata:rowOrder="6">
<Id>10</Id>
<Name>熊宸 </Name>
<Age>25 </Age>
<Grade>硕士 </Grade>
</Student>
<Student diffgr:id="Student8" msdata:rowOrder="7">
<Id>11</Id>
<Name>灵月如 </Name>
<Age>16 </Age>
<Grade>高中 </Grade>
</Student>
<Student diffgr:id="Student9" msdata:rowOrder="8">
<Id>13</Id>
<Name>着 </Name>
<Age>1 </Age>
<Grade>3 </Grade>
</Student>
<Student diffgr:id="Student10" msdata:rowOrder="9">
<Id>14</Id>
<Name>55 </Name>
<Age>55 </Age>
<Grade>55 </Grade>
</Student>
<Student diffgr:id="Student11" msdata:rowOrder="10">
<Id>16</Id>
<Name>superbomb </Name>
<Age>12 </Age>
<Grade>11 </Grade>
</Student>
<Student diffgr:id="Student12" msdata:rowOrder="11">
<Id>22</Id>
<Name>Chrimas </Name>
<Age>22 </Age>
<Grade>10 </Grade>
</Student>
<Student diffgr:id="Student13" msdata:rowOrder="12">
<Id>555</Id>
<Name>wcf </Name>
<Age>1 </Age>
<Grade>10 </Grade>
</Student>
<Student diffgr:id="Student14" msdata:rowOrder="13">
<Id>666</Id>
<Name>superbomb </Name>
<Age>1 </Age>
<Grade>999 </Grade>
</Student>
<Student diffgr:id="Student15" msdata:rowOrder="14">
<Id>1234</Id>
<Name>1 </Name>
<Age>2 </Age>
<Grade>3 </Grade>
</Student>
<Student diffgr:id="Student16" msdata:rowOrder="15">
<Id>9876</Id>
<Name>superman </Name>
<Age>10 </Age>
<Grade>9999 </Grade>
</Student>
</NewDataSet>
</diffgr:diffgram>
student3.xml
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<Student>
<Id>1</Id>
<Name>jim </Name>
<Age>11 </Age>
<Grade>5 </Grade>
</Student>
<Student>
<Id>2</Id>
<Name>lucy </Name>
<Age>12 </Age>
<Grade>6 </Grade>
</Student>
<Student>
<Id>3</Id>
<Name>ben </Name>
<Age>8 </Age>
<Grade>3 </Grade>
</Student>
<Student>
<Id>4</Id>
<Name>clark </Name>
<Age>5 </Age>
<Grade>2 </Grade>
</Student>
<Student>
<Id>5</Id>
<Name>bob </Name>
<Age>12 </Age>
<Grade>6 </Grade>
</Student>
<Student>
<Id>6</Id>
<Name>bomb </Name>
<Age>11 </Age>
<Grade>8 </Grade>
</Student>
<Student>
<Id>10</Id>
<Name>熊宸 </Name>
<Age>25 </Age>
<Grade>硕士 </Grade>
</Student>
<Student>
<Id>11</Id>
<Name>灵月如 </Name>
<Age>16 </Age>
<Grade>高中 </Grade>
</Student>
<Student>
<Id>13</Id>
<Name>着 </Name>
<Age>1 </Age>
<Grade>3 </Grade>
</Student>
<Student>
<Id>14</Id>
<Name>55 </Name>
<Age>55 </Age>
<Grade>55 </Grade>
</Student>
<Student>
<Id>16</Id>
<Name>superbomb </Name>
<Age>12 </Age>
<Grade>11 </Grade>
</Student>
<Student>
<Id>22</Id>
<Name>Chrimas </Name>
<Age>22 </Age>
<Grade>10 </Grade>
</Student>
<Student>
<Id>555</Id>
<Name>wcf </Name>
<Age>1 </Age>
<Grade>10 </Grade>
</Student>
<Student>
<Id>666</Id>
<Name>superbomb </Name>
<Age>1 </Age>
<Grade>999 </Grade>
</Student>
<Student>
<Id>1234</Id>
<Name>1 </Name>
<Age>2 </Age>
<Grade>3 </Grade>
</Student>
<Student>
<Id>9876</Id>
<Name>superman </Name>
<Age>10 </Age>
<Grade>9999 </Grade>
</Student>
</NewDataSet>
XML的序列化和反序列化
DEMO
//类文件的序列化和反序列化
//序列化
book book1 = new book();
book1.Id = 1;
book1.Name = "十万个为什么";
book1.Price = "55";
book1.Publisher = "安童生聚乐部";
book1.PublisherTime = "2011年12月12日";
Console.WriteLine(book1.ToString());
XmlWriter writer = XmlWriter.Create("d:\\bookserialization.xml");
XmlSerializer serializaer = new XmlSerializer(typeof(book));
serializaer.Serialize(writer, book1);
writer.Close();
//反序列化
XmlSerializer serializer = new XmlSerializer(typeof(book));
XmlReader reader=XmlReader.Create("d:\\bookserialization.xml");
book tmp = (book)serializer.Deserialize(reader);
reader.Close();
Console.WriteLine(tmp.ToString());
执行结果
bookserialization.xml
<?xml version="1.0" encoding="utf-8"?>
<book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" id="1">
<name>十万个为什么</name>
<price>55</price>
<publisher>安童生聚乐部</publisher>
<publishertime>2011年12月12日</publishertime>
</book>
序列化结果