Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Data type lost when convertingto XML

Data type lost when convertingto XML

Scheduled Pinned Locked Moved C#
data-structuresxmljsonhelpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    Yaron K
    wrote on last edited by
    #1

    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

    H 1 Reply Last reply
    0
    • Y Yaron K

      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

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      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 a DataSet, the DataSet has no idea what it is reading and will assume that all the columns are simply strings. In order to coerce the DataSet to use a schema, you can either create a DataSet schema using the DataSet designer in VS.NET, or just type one manually yourself and read that schema in with DataSet.ReadXmlSchema, then use DataSet.ReadXml. An even better way is to create a strongly-typed DataSet using the VS.NET DataSet designer (right-click on project or project folder, select Add->Add New Item and select DataSet) and use that instead of DataSet:

      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-----

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups