// Read the XML document back in. // Create new FileStream to read schema with. System.IO.FileStream fsReadXml = new System.IO.FileStream (xmlFilename, System.IO.FileMode.Open); // Create an XmlTextReader to read the file. System.Xml.XmlTextReader myXmlReader = new System.Xml.XmlTextReader(fsReadXml); // Read the XML document into the DataSet. newDataSet.ReadXml(myXmlReader); // Close the XmlTextReader myXmlReader.Close();-------------------- use the method you can read xml to dataset
gracejty(VS.NET妹妹) 怎么样把DataSet的内容装换成xml文
DataSet.WriteXml方法 private void WriteXmlToFile(DataSet thisDataSet) { if (thisDataSet == null) { return; } // Create a file name to write to. string filename = "myXmlDoc.xml"; // Write to the file with the WriteXml method. thisDataSet.WriteXml(filename); }
OK吗??
using System.IO;
using System.Data;
using System.Xml;namespace HowTo.Samples.XML
{public class LoadDataSetXMLDataSample
{
private const String document = "books.xml";
private const String myLoadSchema = "books.xsd";
private XmlDataDocument myXmlDataDocument; public static void Main()
{
String[] args = {document, myLoadSchema};
LoadDataSetXMLDataSample myLoadDataSetXMLDataSample = new LoadDataSetXMLDataSample();
myLoadDataSetXMLDataSample.Run(args);
} public void Run(String[] args)
{
try
{
Console.WriteLine("Creating an XmlDataDocument ...");
myXmlDataDocument = new XmlDataDocument();
ParseSchema(args[1]);
DisplayTableStructure(); myXmlDataDocument.Load(args[0]); DisplayTables(myXmlDataDocument.DataSet);
}
catch (Exception e)
{
Console.WriteLine ("Exception: {0}", e.ToString());
}
} // Loads a specified schema into the DataSet
public void ParseSchema(String schema)
{
StreamReader myStreamReader = null; try
{
Console.WriteLine("Reading Schema file ...");
myStreamReader = new StreamReader(schema);
myXmlDataDocument.DataSet.ReadXmlSchema(myStreamReader);
} catch (Exception e)
{
Console.WriteLine ("Exception: {0}", e.ToString());
} finally
{
if (myStreamReader != null)
myStreamReader.Close();
}
} // Displays the DataSet tables structure
private void DisplayTableStructure()
{
Console.WriteLine("Table structure \r\n");
Console.WriteLine("Tables count=" + myXmlDataDocument.DataSet.Tables.Count.ToString());
for (int i = 0; i < myXmlDataDocument.DataSet.Tables.Count; i++)
{
Console.WriteLine("\r\nTableName='" + myXmlDataDocument.DataSet.Tables[i].TableName + "'.");
Console.WriteLine("Columns count=" + myXmlDataDocument.DataSet.Tables[i].Columns.Count.ToString()); for (int j = 0; j < myXmlDataDocument.DataSet.Tables[i].Columns.Count; j++)
{
Console.WriteLine("\tColumnName='" + myXmlDataDocument.DataSet.Tables[i].Columns[j].ColumnName + "', type = " + myXmlDataDocument.DataSet.Tables[i].Columns[j].DataType.ToString());
}
}
} // Displays the contents of the DataSet tables
private void DisplayTables(DataSet dataset)
{
// Navigate Dataset
Console.WriteLine("\r\nContent of Tables ..."); foreach(DataTable table in dataset.Tables)
{
Console.WriteLine("TableName = " + table.TableName);
Console.WriteLine ("{0}", "---------");
Console.WriteLine("Columns ..."); foreach(DataColumn column in table.Columns)
{
Console.Write("{0,-22}",column.ColumnName);
}
Console.WriteLine("\r\nNumber of rows = {0}", table.Rows.Count.ToString());
Console.WriteLine("Rows ..."); foreach(DataRow row in table.Rows)
{
foreach(Object value in row.ItemArray)
{
Console.Write("{0,-22}",value.ToString());
}
Console.WriteLine();
}
Console.WriteLine();
}
}} // End class LoadDataSetXMLDataSample
} // End namespace HowTo.Samples.XML
/////////////////////////////////
<?xml version='1.0'?>
<!-- This file represents a fragment of a book store inventory database -->
<bookstore>
<book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
<title>The Gorgias</title>
<author>
<first-name>Sidas</first-name>
<last-name>Plato</last-name>
</author>
<price>9.99</price>
</book>
</bookstore>
//////////////////////<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"> <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>
// Read the XML document back in.
// Create new FileStream to read schema with.
System.IO.FileStream fsReadXml = new System.IO.FileStream
(xmlFilename, System.IO.FileMode.Open);
// Create an XmlTextReader to read the file.
System.Xml.XmlTextReader myXmlReader =
new System.Xml.XmlTextReader(fsReadXml);
// Read the XML document into the DataSet.
newDataSet.ReadXml(myXmlReader);
// Close the XmlTextReader
myXmlReader.Close();--------------------
use the method you can read xml to dataset
怎么样把DataSet的内容装换成xml文
private void WriteXmlToFile(DataSet thisDataSet) {
if (thisDataSet == null) { return; }
// Create a file name to write to.
string filename = "myXmlDoc.xml";
// Write to the file with the WriteXml method.
thisDataSet.WriteXml(filename);
}
这个方法就可以全部读入,但是好么???那样有意义吗??全部读入???