0
<?xml version="1.0" encoding="UTF-8"?>
<NewDataSet>
  <xs:schema xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:xs="http://www.w3.org/2001/XMLSchema" id="NewDataSet">
    <xs:element msdata:IsDataSet="true" msdata:UseCurrentLocale="true" name="NewDataSet">
      <xs:complexType>
        <xs:choice maxOccurs="unbounded" minOccurs="0">
          <xs:element name="Table1">
            <xs:complexType>
              <xs:sequence>
                <xs:element minOccurs="0" name="CODE" type="xs:string"/>
                <xs:element minOccurs="0" msdata:DateTimeMode="Unspecified" name="MAXTIME" type="xs:dateTime"/>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <Table1>
    <CODE>A112</CODE>
    <MAXTIME/>
  </Table1>
  <Table1>
    <CODE>A113</CODE>
    <MAXTIME>2018-03-13T00:00:00</MAXTIME>
  </Table1>
</NewDataSet>

I want to convert xml string above to Dataset with this schema, but when I using that code below:

DataSet dataSet = new DataSet();
dataSet.ReadXml(xmlPath, XmlReadMode.ReadSchema);

it throw exception because <MAXTIME> field is empty

The string '' is not a valid AllXsd value.

So how to convert it with null able Datetime field without changing the schema?

I can not change the input xml file, as it is provided by my customer, so I need a solution besides adding attributes to the fields.

jimbo R
  • 253
  • 2
  • 3
  • 14
  • An empty string isn't null. An empty element isn't the same as missing, no or null element. You *did* define an empty `maxtime` element in your XML. If you wanted it to be NULL, it should have the `xsi:nil='true'` attribute or it shouldn't be there at all – Panagiotis Kanavos Apr 26 '18 at 07:39
  • Perhaps you should create a typed dataset first and check the generated XSD and XML files? – Panagiotis Kanavos Apr 26 '18 at 07:45
  • i cant change the input XML, it from my customer – jimbo R Apr 26 '18 at 07:46
  • Then change the XSD. Or tell the customer to change it. Or *ignore* the XSD, use your own typed dataset. That's invalid XML according to the XSD. Even better, don't use a dataset at all, deserializet to classes and serializers generated from that XSD using `svcutil.exe` or `xsd.exe` – Panagiotis Kanavos Apr 26 '18 at 07:50
  • can u show me how Deserialize xml into classes, i tried but error because my xml have inline schema: `XmlSerializer serializer = new XmlSerializer(typeof(List)); StreamReader reader = new StreamReader(path); var lst = (List)serializer.Deserialize(reader);` – jimbo R Apr 26 '18 at 08:02

0 Answers0