怎样用XML技术对数据库进行操作

众所周知,XML文件是保存和传输数据的载体,数据库自然也可以用XML进行读取。那么怎样使用XML技术呢?Microsoft.Net提供了XmlDataDocument类来专门来操作数据库。

XmlD

ataDocument是XmlDocument的一个子类,它可以去读取和操作数据库或Xml文件中的数据,它使用的是W3C Document Object(DOM)。DOM用带有层次的节点来描述数据。由于它实现了IxpathNavigable接口,所以它又可以被XslTransform使用。由于XmlDataDocument和DataSet有密切的联系,任何对XmlDataDocument的改动将映射到相应的DataSet上,反之亦然。

下面我们用一个例子来说明怎样将一个DataSet输出到一个Xml文件中去。

WriteTo.cs

using System;

using System.Data;

using System.Xml;

using System.Data.SqlClient;


public class WriteTo

{

public static void Main()

{

DataSet ds 
= new DataSet();// 新建一个DataSet,也可以使用已存在的数据库。

DataTable dt 
= new DataTable("product");//新建一个表。

DataColumn dc1 
= new DataColumn("Id",System.Type.GetType("System.Int32"));

DataColumn dc2 
= new DataColumn("ProductName",System.Type.GetType("System.String"));

dt.Columns.Add(dc1);

dt.Columns.Add(dc2);

for(int i=1;i<5;i++)

{

DataRow dr 
= dt.NewRow();

dr[
"Id"= i;

dr[
"ProductName"= "Product"+i.ToString();

dt.Rows.Add(dr);

}


ds.Tables.Add(dt);


//Load the document with the DataSet.

XmlDataDocument doc 
= new XmlDataDocument(ds); 


//Write data to a file.

XmlTextWriter writer 
= new XmlTextWriter(Console.Out);//这时我们将结果输出到Dos下以方便调试,也可以输出到一个XML文件中。

writer.Formatting 
= Formatting.Indented;//输出格式。

doc.WriteTo(writer); 

doc.WriteContentTo(writer);

}


}



运行这个程序,我们发现结果如下:

<NewDataSet>

<product>

<Id>1</Id>

<ProductName>Product1</ProductName>

</product>

<product>

<Id>2</Id>

<ProductName>Product2</ProductName>

</product>

<product>

<Id>3</Id>

<ProductName>Product3</ProductName>

</product>

<product>

<Id>4</Id>

<ProductName>Product4</ProductName>

</product>

</NewDataSet>

<NewDataSet>

<product>

<Id>1</Id>

<ProductName>Product1</ProductName>

</product>

<product>

<Id>2</Id>

<ProductName>Product2</ProductName>

</product>

<product>

<Id>3</Id>

<ProductName>Product3</ProductName>

</product>

<product>

<Id>4</Id>

<ProductName>Product4</ProductName>

</product>

</NewDataSet>

我们可以看到一个数据库成功的输出成一个XML文件格式。

有了XmlDataDocument这个类,我们可以很轻松的对一个数据库进行操作,并且我们对XmlDataDocument所做的操作都会映射到数据库中去。

比如我们对一个XML文件操作(这个XML文件是DataSet产生的),相应的改变也会导致DataSet的改变。

下面我们以一个实例来说明这一点,需说明的一点是,这个实例还使用了XSD进行校验,以确保数据库传输和操作的正确性。

Store.xsd:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name="bookstore" type="bookstoreType"/>

<xsd:complexType name="bookstoreType">

<xsd:sequence maxOccurs="unbounded">

<xsd:element name="book" type="bookType"/>

</xsd:sequence>

</xsd:complexType>


<xsd:complexType name="bookType">

<xsd:sequence>

<xsd:element name="title" type="xsd:string"/>

<xsd:element name="author" type="authorName"/>

<xsd:element name="price" type="xsd:decimal"/>

</xsd:sequence>

<xsd:attribute name="genre" type="xsd:string"/>

</xsd:complexType>


<xsd:complexType name="authorName">

<xsd:sequence>

<xsd:element name="first-name" type="xsd:string"/>

<xsd:element name="last-name" type="xsd:string"/>

</xsd:sequence>

</xsd:complexType>

</xsd:schema>

我们可以发现这个XSD文件控制的XML文件的格式,它定义了三个元素:title,author和price。

对应的DataSet.xml如下:

<!--sample XML fragment-->

<bookstore>

<book genre='novel' ISBN='10-861003-324'>

<title>The Handmaid's Tale</title>

<price>19.95</price>

</book>

<book genre='novel' ISBN='1-861001-57-5'>

<title>Pride And Prejudice</title>

<price>24.95</price>

</book>

</bookstore>

这个Xml文件可以向上例那样通过XmlDataDocument产生,数据库名是:bookstore,表名是book,column名是title和price,genre和ISBN也是当做column来处理。

针对这个Xml文件我们可以进行操作。

/**

* System.Xml/XmlDataDocument class

* Author: wenjun

*/


using System;

using System.Data;

using System.Xml;

public class GetRowFromElement

{

public static void Main() 

{

XmlDataDocument doc 
= new XmlDataDocument(); 

doc.DataSet.ReadXmlSchema(
"store.xsd");

doc.Load(
"dataset.xml");

//Change the price on the first book.

XmlElement root 
= doc.DocumentElement;

DataRow row 
= doc.GetRowFromElement((XmlElement)root.FirstChild);

row[
"price"= "18.95";//将第一column中的price值改掉

Console.WriteLine(
"Display the modified XML data");

Console.WriteLine(doc.DocumentElement.OuterXml);

}


}


运行这个程序,发现输出如下:

Display the modified XML data...

 

<bookstore><book genre="novel" ISBN="10-861003-324"><title>The Handmaid's Tale</title><price>18.95</price></book><book genre="novel" ISBN="1-861001-57-5"><title>Pride And Prejudice</title><price>24.95</price></book></bookstore>

 

我们可以发现price的值已改了过来。我们可以在查找相应数据库(bookstore),发现里面的值也跟着改了。

原文地址:https://www.cnblogs.com/ryb/p/484977.html