none
Questions on SchemaSerializationMode.IncludeSchema and broken backwards compatibility when adding columns to a DataTable RRS feed

  • Question

  • We have a number of .NET DataSets that we serialize out to XML files and de-serialize them back in. And, of course, backward compatibility is important. In the past, adding columns has never been a problem... if missing in the serialized out data in the file, on import it is there with its default value. Perfect.

    We are now experiencing a problem though... we added a column and that column is NOT getting added during de-serialization... it is just missing... even though it is a typed dataset and the auto-generated code just assumes the column is there.

    In debugging this, the only difference between DataSets where this works and where it does NOT work is that the working ones have SchemaSerializationMode set to ExcludeSchema, whereas the broken ones mistakenly have it set to IncludeSchema. We intended to always ExcludeSchema just to avoid wasting time and space. But it seems there was a more important reason: if you IncludeSchema, then you break backwards compatibility because it deserializes it with the old schema rather than the current schema.

    Questions:

    1. Is there a way to tell it to ignore any serialized schema when deserializing?

    2. Is there a way to tell it to upgrade an instantiated dataset to the current schema after deserializing?

    3. Is there an easy way to strip the schema information out of the XML files?

    4. How is backward compatibility with typed datasets supposed to work with IncludeSchema?

    Any guidance on why our new columns are not showing up would be greatly appreciated!

    Tuesday, April 25, 2017 3:31 PM

All replies

  • Hi TCC Developer,

    >>Is there a way to tell it to ignore any serialized schema when deserializing?

    Ignores any inline schema and loads the data into the existing DataSet schema. Any data that does not match the existing schema is discarded. If no schema exists in the DataSet, no data is loaded.

    DataSet dataSet = new DataSet();  
    DataTable dataTable = new DataTable("table1");  
    dataTable.Columns.Add("col1", typeof(string));  
    dataSet.Tables.Add(dataTable);  
      
    string xmlData = "<XmlDS><table1><col1>Value1</col1></table1><table1><col1>Value2</col1></table1></XmlDS>";  
      
    System.IO.StringReader xmlSR = new System.IO.StringReader(xmlData);  
      
    dataSet.ReadXml(xmlSR, XmlReadMode.IgnoreSchema);  

    >>Is there a way to tell it to upgrade an instantiated dataset to the current schema after deserializing?

    Please read schema information from xml and update to dataset.

    >>Is there an easy way to strip the schema information out of the XML files?

    you could get schema information via xsd.exe. for more information, please refer to:

    https://msdn.microsoft.com/en-us/library/x6c1kb0s%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    Best regards,

    Cole Wu


    MSDN Community Support<br/> Please remember to click &quot;Mark as Answer&quot; the responses that resolved your issue, and to click &quot;Unmark as Answer&quot; if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact <a href="mailto:MSDNFSF@microsoft.com">MSDNFSF@microsoft.com</a>.


    Friday, April 28, 2017 5:38 AM
    Moderator
  • I typically use XmlReadMode.InferSchema and that works pretty well. My serialized XML doesn't contain a schema though, it simply contains the data, by creating the XML file without a schema (ds.WriteXml("MyFileName")). How are *you* serializing it *to* XML? And do you actually need the schema to be in your XML file? If you're serializing to-and-from the same Typed DataSets, you definitely do *not* need to include the schema in the XML. But if you used InferSchema, it wouldn't matter anyway.

    Here's is what the documentation says about InferSchema:

    >>Ignores any inline schema, infers schema from the data and loads the data. If the DataSet already contains a schema, the current schema is extended by adding new tables or adding columns to existing tables. An exception is thrown if the inferred table already exists but with a different namespace, or if any of the inferred columns conflict with existing columns.<<


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Friday, April 28, 2017 2:29 PM