Følgende eksemple illusterer nogle af mulighederne.
Eksemplet er med en database, men alt med DataSet må kunne anvendes
på din problem stilling.
Kode:
using System;
using System.IO;
using System.Xml;
using System.Xml.Xsl;
using System.Data;
using System.Data.SqlClient;
class MainClass
{
private static SqlConnection con;
public static void Main(string[] args)
{
con = new SqlConnection("server=ARNEPC2\\ARNEPC2RUN;Integrated Security=SSPI;database=TestMSDE");
con.Open();
test_nonxml();
test_sqlsrvxml();
test_dsxml();
test_dsxmltrans();
test_dsxmlschema();
con.Close();
}
private static void test_nonxml()
{
Console.WriteLine("non XML");
Console.WriteLine("=======");
SqlCommand cmd = new SqlCommand("SELECT * FROM T1", con);
SqlDataReader rdr = cmd.ExecuteReader();
while(rdr.Read())
{
int f1 = (int)rdr[0];
string f2 = (string)rdr[1];
Console.WriteLine(f1 + " " + f2);
}
rdr.Close();
Console.WriteLine();
}
private static void test_sqlsrvxml()
{
Console.WriteLine("SQLServer XML");
Console.WriteLine("=============");
SqlCommand cmd = new SqlCommand("SELECT * FROM T1 FOR XML AUTO", con);
XmlReader rdr = cmd.ExecuteXmlReader();
rdr.Read();
String s;
for(;;) {
s = rdr.ReadOuterXml();
if(s == "") break;
Console.WriteLine(s);
}
rdr.Close();
Console.WriteLine();
}
private static void test_dsxml()
{
Console.WriteLine("Data set XML");
Console.WriteLine("============");
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM T1", con);
DataSet ds = new DataSet("T1");
da.Fill(ds, "record");
ds.WriteXml(Console.Out);
Console.WriteLine();
}
private static void test_dsxmltrans()
{
Console.WriteLine("Data set XML transformed");
Console.WriteLine("========================");
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM T1", con);
DataSet ds = new DataSet("T1");
da.Fill(ds, "record");
XmlDocument doc = new XmlDocument();
doc.LoadXml(ds.GetXml());
XslTransform xslt = new XslTransform();
xslt.Load("..\\..\\T1.xsl");
XmlTextWriter wrt = new XmlTextWriter(Console.Out);
wrt.Formatting = Formatting.Indented;
xslt.Transform(doc, null, wrt, null);
Console.WriteLine();
}
private static void test_dsxmlschema()
{
Console.WriteLine("Data set XML with schema");
Console.WriteLine("========================");
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM T1", con);
DataSet ds = new DataSet("T1");
ds.ReadXmlSchema("..\\..\\T1.xsd");
da.Fill(ds, "record");
ds.WriteXml(Console.Out);
Console.WriteLine();
}
}
T1.xsl:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="
http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<allrecords>
<xsl:for-each select="//record">
<record>
<xsl:attribute name="f1"><xsl:value-of select="F1"/></xsl:attribute>
<xsl:attribute name="f2"><xsl:value-of select="F2"/></xsl:attribute>
</record>
</xsl:for-each>
</allrecords>
</xsl:template>
</xsl:stylesheet>
T1.xsd:
<?xml version="1.0"?>
<xsd:schema version="1.0" xmlns:xsd="
http://www.w3.org/2001/XMLSchema"> <xsd:element name="allrecords">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="record">
<xsd:complexType>
<xsd:attribute name="f1" type="xsd:integer"/>
<xsd:attribute name="f2" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Output:
non XML
=======
1 A
2 BB
3 CCC
SQLServer XML
=============
<T1 F1="1" F2="A" />
<T1 F1="2" F2="BB" />
<T1 F1="3" F2="CCC" />
Data set XML
============
<T1>
<record>
<F1>1</F1>
<F2>A</F2>
</record>
<record>
<F1>2</F1>
<F2>BB</F2>
</record>
<record>
<F1>3</F1>
<F2>CCC</F2>
</record>
</T1>
Data set XML transformed
========================
<allrecords>
<record f1="1" f2="A">
</record>
<record f1="2" f2="BB">
</record>
<record f1="3" f2="CCC">
</record>
</allrecords>
Data set XML with schema
========================
<allrecords>
<record f1="1" f2="A" />
<record f1="2" f2="BB" />
<record f1="3" f2="CCC" />
</allrecords>
Hvis dit ønskede output er tilpas simpelt synes jeg at løsningen med schema
er meget elegant !