none
Using ReadXml ignored DefaultValue (column not present in xml) RRS feed

  • Question

  • Hi all

    I have an XML file here and I have a datatable with columns to represent those elements, but also some additional columns NOT present in the XML. I'd like to set a default value for these columns so that when reading in the XML, the row is populated with the default value (the value cannot come from the XML because it will NEVER be present ion the XML)

    I do understand why it is left null and the DataColumn.DefaultValue is ignored (at least, I can explain it in a way that makes sense to me - readxml is supposed to deser and rep[resent what is on disk - these columns are rightfully null because their value is unknown always) but i wonder if there is a way to allow the default values in?

    At the moment, i'm taking the simplistic route of iterating the datatable and setting them after the read

    Thanks
    Tuesday, August 11, 2009 8:23 PM

Answers

  • Hi caius jard,

    When you call datatable.readxml, it will create a new table according to the XML file’s schema. Then load the xml data into this table. At last, load this table into the table you defined. So that’s why you can’t get the default value working. Default value only works when you add a new row it.

    For your problem, you have two options:

    1)    Load each record into the table you defined by your own instead of readxml method.

    2)    Traverse the table to add the default value.

     

    Best Regards

    Yichun Feng

     

    Friday, August 14, 2009 6:41 AM

All replies

  • Use the DataColumn.DefaultValue property to set defaults for your data table when you create it.

    Tuesday, August 11, 2009 8:36 PM
  • I'm guessing you either didnt read my post, or didn't understand it.

    Suppose we have an xml file:

    <a>
      <b>c</b>
      <b>d</b>
    </a>


    And we have a strongly typed datatable:
    Name = a
    Columns = b, z


    And we have code:

    MyDataTable dt = new MyDataTable();
    dt.zColumn.DefaultValue = "z";
    dt.readXml(theFile);

    And we then have the result datatable:
    ___
    b|z
    ---
    c|
    d|
    ---



    Do you understand, that even though I set the DefaultValue before I ReadXml, the xml doesnt contain a value for z, the xml was not created as a result of dt.WriteXml (it is from a remote system), and after deserializing the XML, the datatable has NULL in all the z column, rather than the DefaultValue of "z"
    a
    Wednesday, August 12, 2009 2:17 PM
  • Hi caius jard,

    When you call datatable.readxml, it will create a new table according to the XML file’s schema. Then load the xml data into this table. At last, load this table into the table you defined. So that’s why you can’t get the default value working. Default value only works when you add a new row it.

    For your problem, you have two options:

    1)    Load each record into the table you defined by your own instead of readxml method.

    2)    Traverse the table to add the default value.

     

    Best Regards

    Yichun Feng

     

    Friday, August 14, 2009 6:41 AM