导航

Xml与DataTable相互转换方法

Posted on 2010-04-18 20:50  lilin  阅读(36781)  评论(1编辑  收藏  举报

Xml文件:xmlsample.xml

 

代码
<music>
<song>
<artist>The Chi-lites</artist>
<genre>Soul</genre>
<album>A lonely man</album>
<year>1972</year>
</song>
<song>
<artist>Babyface</artist>
<genre>R&amp;B</genre>
<album>unknown</album>
<year></year>
</song>
<song>
<artist>Babyface</artist>
<genre>R&amp;B</genre>
<album>The essential babyface</album>
<year>2001</year>
</song>
<song>
<artist>Babyface</artist>
<genre>R&amp;B</genre>
<album>Grown and sexy</album>
<year>2005</year>
</song>
<song>
<artist>Maria Arredondo</artist>
<genre>Pop</genre>
<album>Not going under</album>
<year>2004</year>
</song>
<song>
<artist>Leona Lewis</artist>
<genre>Pop</genre>
<album>Unknown</album>
<year>2008</year>
</song>
<song>
<artist>Usher</artist>
<genre>R&amp;B</genre>
<album>Usher</album>
<year>2008</year>
</song>
<song>
<artist>Christina Aguilera</artist>
<genre>Blues</genre>
<album>Back to basics</album>
<year>2004</year>
</song>
<song>
<artist>Sting</artist>
<genre>Pop</genre>
<album>Shape of my heart</album>
<year></year>
</song>
</music>

 

代码文件:

 

代码
class Program
{
static void Main(string[] args)
{
VaildationXmlSchema();
}

// Xml结构的文件读到DataTable中
static DataTable XmlToDataTableByFile()
{
string fileName = "E:\\xmlsample.xml";
XmlDocument doc
= new XmlDocument();
doc.Load(fileName);

DataTable dt
= new DataTable("song");
//以第一个元素song的子元素建立表结构
XmlNode songNode = doc.SelectSingleNode("/music/song[1]");
string colName;
if (songNode != null)
{
for (int i = 0; i < songNode.ChildNodes.Count; i++)
{
colName
= songNode.ChildNodes.Item(i).Name;
dt.Columns.Add(colName);
}
}
DataSet ds
= new DataSet("music");
ds.Tables.Add(dt);

//Xml所有song元素的子元素读到表song中,当然用dt也可以读。
ds.ReadXml(fileName);
return dt;
}

// Xml结构的字符中读到DataTable中
static void XmlToDataTableByString()
{
string fileName = "E:\\xmlsample.xml";
XmlDocument doc
= new XmlDocument();
doc.Load(fileName);

DataTable dt
= new DataTable("song");
//以第一个元素song的子元素建立表结构
XmlNode songNode = doc.SelectSingleNode("/music/song[1]");
string colName;
if (songNode != null)
{
for (int i = 0; i < songNode.ChildNodes.Count; i++)
{
colName
= songNode.ChildNodes.Item(i).Name;
dt.Columns.Add(colName);
}
}
DataSet ds
= new DataSet();
ds.Tables.Add(dt);

//获取Xml字串
string xmlString = doc.InnerXml;
StringReader sr
= new StringReader(xmlString);
XmlTextReader xr
= new XmlTextReader(sr);
//Xml所有song元素的子元素读到表song中,当然用dt也可以读。
ds.ReadXml(xr);
}

// DataTable转换成Xml结构的文本
static void DataTableToXml()
{
//dt的名为song,ds的名为music
DataTable dt = XmlToDataTableByFile();

//保存Xml验证架构
dt.WriteXmlSchema("E://xmlsample.xsd");

//dt写成Xml结构
System.IO.TextWriter tw = new System.IO.StringWriter();
dt.WriteXml(tw);
string xml = tw.ToString();
}

//验证Xml结构
static void VaildationXmlSchema()
{
XmlSchemaSet
set = new XmlSchemaSet();
set.Add("", "E:\\xmlsample.xsd");

XmlDocument doc
= new XmlDocument();
doc.Schemas
= set;

try
{
doc.Load(
"E:\\xmlsample.xml");
doc.Validate(
new ValidationEventHandler(Vaildation));
}
catch (Exception e)
{
Console.Write(e.Message);
}
}
static void Vaildation(object sender, ValidationEventArgs e)
{
switch (e.Severity)
{
case XmlSeverityType.Error:
throw e.Exception;
case XmlSeverityType.Warning:
throw e.Exception;
}
}
}

 

生成的验证Xml文件:xmlsample.xsd

 

代码
<?xml version="1.0" standalone="yes"?>
<xs:schema id="music" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="music" msdata:IsDataSet="true" msdata:MainDataTable="song" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="song">
<xs:complexType>
<xs:sequence>
<xs:element name="artist" type="xs:string" minOccurs="0" />
<xs:element name="genre" type="xs:string" minOccurs="0" />
<xs:element name="album" type="xs:string" minOccurs="0" />
<xs:element name="year" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>

原文件下载