Data type lost when convertingto XML
-
Hi I am writing code which accepts an array of objects, converts them to XML and loads them into a dataset. My problem is that when the XML is created, it does not contain an indication of the data types of the fields. As a result, all the data types of the columns in the dataset are System.String. Is there a way to force creation of shcemea when creating the XML string? Here is the code: [Serializable] [XmlType("MyClass")] public class MyClass { [XmlAttribute("IntField")] public int IntField; [XmlAttribute("StringField")] public string StringField; [XmlAttribute("ByteField")] public byte ByteField; public MyClass() {} } string xmlString; System.Xml.Serialization.XmlSerializer xmlSer; XmlWriter xmlWriter; MyClass myObj = new MyClass(); StringWriter sw = new StringWriter(); myObj.IntField = 1; myObj.StringField = "a string..."; myObj.ByteField = 2; xmlWriter = new XmlTextWriter(sw); xmlSer = new XmlSerializer(typeof(MyClass)); xmlSer.Serialize(xmlWriter,myObj); xmlString = sw.ToString(); // at this point the xmlString looks as follows: //" //" DataSet ds = new DataSet(); StringReader sr = new StringReader(xmlString); ds.ReadXml(sr); // At this point ds.Tables[0].Columns[0].DataType.ToString() returns System.String instead of System.32
-
Hi I am writing code which accepts an array of objects, converts them to XML and loads them into a dataset. My problem is that when the XML is created, it does not contain an indication of the data types of the fields. As a result, all the data types of the columns in the dataset are System.String. Is there a way to force creation of shcemea when creating the XML string? Here is the code: [Serializable] [XmlType("MyClass")] public class MyClass { [XmlAttribute("IntField")] public int IntField; [XmlAttribute("StringField")] public string StringField; [XmlAttribute("ByteField")] public byte ByteField; public MyClass() {} } string xmlString; System.Xml.Serialization.XmlSerializer xmlSer; XmlWriter xmlWriter; MyClass myObj = new MyClass(); StringWriter sw = new StringWriter(); myObj.IntField = 1; myObj.StringField = "a string..."; myObj.ByteField = 2; xmlWriter = new XmlTextWriter(sw); xmlSer = new XmlSerializer(typeof(MyClass)); xmlSer.Serialize(xmlWriter,myObj); xmlString = sw.ToString(); // at this point the xmlString looks as follows: //" //" DataSet ds = new DataSet(); StringReader sr = new StringReader(xmlString); ds.ReadXml(sr); // At this point ds.Tables[0].Columns[0].DataType.ToString() returns System.String instead of System.32
XML serialization using the
XmlSerializer
always serializes to strings (that's all XML can contains) but deserializes the string back to the Types of the properties with which they map. When you read this XML fragment into aDataSet
, theDataSet
has no idea what it is reading and will assume that all the columns are simply strings. In order to coerce theDataSet
to use a schema, you can either create aDataSet
schema using theDataSet
designer in VS.NET, or just type one manually yourself and read that schema in withDataSet.ReadXmlSchema
, then useDataSet.ReadXml
. An even better way is to create a strongly-typedDataSet
using the VS.NETDataSet
designer (right-click on project or project folder, select Add->Add New Item and select DataSet) and use that instead ofDataSet
:MyDataSet ds = new MyDataSet();
StringReader sr = new StringReader(xmlString);
ds.ReadXml(sr);This way, the
DataSet
object is initialized with all the necessary table and column information and you can refer to columns by name without having to cast to the necessary data type.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----