none
ReadXml, Xml DataSet, Empty String to null RRS feed

  • Question

  • I am using ReadXml() with Schema.

                        ds.ReadXmlSchema(Path.Combine( AppDomain.CurrentDomain.BaseDirectory, "MyDataSchema.xml"));
                        ds.ReadXml(stream);

    One data field/element is defined in schema as 

                  <xs:element name="Sales" type="xs:string" minOccurs="0" />

    I got the data from outside source. The data I got is the empty element like <Sales />. This causes an empty string being read into the dataset. So I have to check if it is empty string before I cast it to a float number and save it to DB.

    I am wondering,

    Is there a way I can set in the schema so the empty element be automatically converted to null when I call GetXml()?

    Thanks,

    John


    • Edited by Q John Chen Friday, March 4, 2016 8:09 PM typo
    Friday, March 4, 2016 8:08 PM

Answers

All replies

  • John, if it was null, you'd still have to check it before you cast or convert it. So, I don't think that will help at all.

    ~~Bonnie DeWitt [C# MVP]

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

    Saturday, March 5, 2016 2:41 AM
  • Bonnie,

    I first dump (SqlBulkCopy) to DB (the added benefit of having a record of the source data). The rest is in stored procedure that moves the data to different tables.

    You can cast null to any data type. In my case, it is cast(Sales as float). 

    I is not the end of the world though. I can pre-process the dataset before I move the data to DB. I just though it would be neat setting it up in the schema.

    Thanks,

    John

    Saturday, March 5, 2016 3:10 AM
  • John,

    I'm not an XML expert, so I don't know for sure (and have not taken the time to test it), but I read this on StackOverflow about using a default: http://stackoverflow.com/questions/21755856/how-to-define-a-default-value-for-an-option-element-using-xsd?rq=1
    (and he had a link to this: http://www.w3.org/TR/xmlschema-0/#ref36)

    Basically, it says this:
    When an element is declared with a default value, the value of the element is whatever value appears as the element's content in the instance document; if the element appears without any content, the schema processor provides the element with a value equal to that of the default attribute. However, if the element does not appear in the instance document, the schema processor does not provide the element at all. In summary, the differences between element and attribute defaults can be stated as: Default attribute values apply when attributes are missing, and default element values apply when elements are empty.

    Anyway, I'm not sure if an empty string would qualify as an empty element, so I don't know if using a default would work, but you could try it:

    <xs:element name="Sales" type="xs:string" minOccurs="0" default="0" />


    ~~Bonnie DeWitt [C# MVP]

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

    Saturday, March 5, 2016 5:20 PM
  • Thanks Bonnie,

    I read the articles in the link. It appears to me the empty is the value.

    Thought give it a try. But I want it to be null -"no value", And I don't know how to specify the null or nil.

    I give up on this and just patiently update the blanks (lots of columns) to null after the data get in to the DB.

    John

    Saturday, March 5, 2016 11:50 PM
  • Sorry, John ... I don't see how you could specify null. That's why I suggested zero in my example. I guess it's a moot point if zero isn't the default that you would want to have, but I wondered if it at least worked ... IOW, does it see the empty string as an empty value and default it to zero? 


    ~~Bonnie DeWitt [C# MVP]

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

    Sunday, March 6, 2016 12:00 AM
  • Bonnie,

    For Sales number, I probably can treat 0 as nothing. There are other data that 0 does mean 0. 

    It did not take too much time for me to pre-process the data after moving the data to db. So I am ok now.

    Thanks again,

    John


    Sunday, March 6, 2016 2:20 AM
  • John, I'm glad that it wasn't *too* painless!  ;0)

    Have fun!  :0)


    ~~Bonnie DeWitt [C# MVP]

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

    Sunday, March 6, 2016 2:33 AM
  • I just had a project with similar issues. Ended up creating all columns with dataType string and then I created the same columns in the same table again with correct types but referencing to the string-columns.

    E.g. when the first string-column in the table is called 'c1' then I created another column 'c1_' with correct target-type and an expression of 'IIF(LEN([c1])=0,NULL,[c1])'. After the readXML I used a dataTableReader to read only all columns with correct type into a new target-Table.

    It may sound complicated, but that was by far the fastest solution at the end.



    Sunday, August 9, 2020 4:29 PM
  • Interesting, Carsten. Thanks for your input!  =0)

    ~~Bonnie DeWitt [C# MVP]

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

    Saturday, September 5, 2020 4:54 AM