none
Problem in Dataset ReadXml() and WriteXml()

    Question

  • I have following xml string

    <?xml version="1.0" encoding="utf-8"?>
    <CompObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
     <SystemName>MyTest</SystemName>
      <SystemValue xsi:nil="true" />
    </CompObject>

    Using below code I am reading it in DataSet.

    //xmlString contains above xml
            using (StringReader stringReader = new StringReader(xmlString))
            {
              DataSet ds = new DataSet();
              //load the StringReader to our DataSet
              ds.ReadXml(stringReader);
            }

    ds.WriteXml("path");//To write xml to file.

    But, the problem is file created at "path" does not matches with the specified string.

    It emits xsi:nil="true" and xmlns:xsi attributes.

    I want exact xml string to be generated.


     

     

     


    Regards,
    Jaydeep
    • Edited by meetjaydeep3 Monday, February 07, 2011 9:54 AM Formatted
    Monday, February 07, 2011 9:52 AM

Answers

  • Hi meetjaydeep3,

     

    The documentation at http://msdn.microsoft.com/en-us/library/ms172138.aspx the following:

     

    the following rules apply for an instance of DataRow.["columnName"] null assignments:

     

    The default default value is DbNull.Value for all except the strongly typed null columns where it is the appropriate strongly typed null value.

     

    Null values are never written out during serialization to XML files (as in "xsi:nil").

     

    All non-null values, including defaults, are always written out while serializing to XML. This is unlike XSD/XML semantics where a null value (xsi:nil) is explicit and the default value is implicit (if not present in XML, a validating parser can get it from an associated XSD schema). The opposite is true for a DataTable : a null value is implicit and the default value is explicit.

     

    All missing column values for rows read from XML input are assigned NULL. Rows created using NewRow or similar methods are assigned the DataColumn's default value.

     

    The IsNull method returns true for both DbNull.Value and INullable.Null .

     

    Base on my understanding, the SystemValue will not be output but set the xsi:nil attribute to false.

     

    If there's any concern, please feel free to let me know.

     

    Have a nice day!


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, February 10, 2011 3:35 AM
    Moderator