serialize DataSet and use WriteXml

T

Tony Johansson

Hi!

Method HandleDataSet can serialize a DataSet or use WriteXml.
If I call this HandleDataSet method and use the statement marked 1 in this
method I simply write the DataSet to a file
in XML format.

If I call this HandleDataSet method and use the statement marked 2 in this
method I serialize the DataSet using the XmlSerializer.

If I then look at the xml files after I have run alternative 1 and 2. They
are very different.

But I can for example use WriteXml first and the use this XML file as input
to deserialize.
I can also first use serialize and use that file as input file to ReadXml
all this works.

But what I find interesting here is why they are so different ?
In one of them did the framework write the XSD schema.
But if I only compare the xml document between alternative 1 and 2 they are
very different. why ?
I have added the xml files from both alternative 1 and 2 at the bottom of
this document.

private void HandleDataSet(string filename)
{
XmlSerializer ser = new XmlSerializer(typeof(DataSet));
DataSet ds = new DataSet("myDataSet");
DataTable dt = new DataTable("myTable");
DataColumn dc = new DataColumn("thing");
dt.Columns.Add(dc); //add DataColumn to DataTable
ds.Tables.Add(dt);
DataRow row;

for (int i = 0; i < 10; i++)
{
row = dt.NewRow();
row[0] = "Thing" + i;
dt.Rows.Add(row);
}

1 ds.WriteXml("DataSet2.xml");
2 TextWriter writer = new StreamWriter(filename);
2 ser.Serialize(writer, ds);
2 writer.Close();
}

1. Used DataSet.WriteXml
=====================
<?xml version="1.0" standalone="yes"?>
<myDataSet>
<myTable>
<thing>Thing0</thing>
</myTable>
<myTable>
<thing>Thing1</thing>
</myTable>
<myTable>
<thing>Thing2</thing>
</myTable>
<myTable>
<thing>Thing3</thing>
</myTable>
<myTable>
<thing>Thing4</thing>
</myTable>
<myTable>
<thing>Thing5</thing>
</myTable>
<myTable>
<thing>Thing6</thing>
</myTable>
<myTable>
<thing>Thing7</thing>
</myTable>
<myTable>
<thing>Thing8</thing>
</myTable>
<myTable>
<thing>Thing9</thing>
</myTable>
</myDataSet>

2. Serialize the DataSet
===============
<?xml version="1.0"?>
<DataSet>
<xs:schema id="myDataSet" xmlns=""
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="myDataSet" msdata:IsDataSet="true"
msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="myTable">
<xs:complexType>
<xs:sequence>
<xs:element name="thing" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<myDataSet>
<myTable diffgr:id="myTable1" msdata:rowOrder="0"
diffgr:hasChanges="inserted">
<thing>Thing0</thing>
</myTable>
<myTable diffgr:id="myTable2" msdata:rowOrder="1"
diffgr:hasChanges="inserted">
<thing>Thing1</thing>
</myTable>
<myTable diffgr:id="myTable3" msdata:rowOrder="2"
diffgr:hasChanges="inserted">
<thing>Thing2</thing>
</myTable>
<myTable diffgr:id="myTable4" msdata:rowOrder="3"
diffgr:hasChanges="inserted">
<thing>Thing3</thing>
</myTable>
<myTable diffgr:id="myTable5" msdata:rowOrder="4"
diffgr:hasChanges="inserted">
<thing>Thing4</thing>
</myTable>
<myTable diffgr:id="myTable6" msdata:rowOrder="5"
diffgr:hasChanges="inserted">
<thing>Thing5</thing>
</myTable>
<myTable diffgr:id="myTable7" msdata:rowOrder="6"
diffgr:hasChanges="inserted">
<thing>Thing6</thing>
</myTable>
<myTable diffgr:id="myTable8" msdata:rowOrder="7"
diffgr:hasChanges="inserted">
<thing>Thing7</thing>
</myTable>
<myTable diffgr:id="myTable9" msdata:rowOrder="8"
diffgr:hasChanges="inserted">
<thing>Thing8</thing>
</myTable>
<myTable diffgr:id="myTable10" msdata:rowOrder="9"
diffgr:hasChanges="inserted">
<thing>Thing9</thing>
</myTable>
</myDataSet>
</diffgr:diffgram>
</DataSet>

//Tony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top