Search code examples
c#xmldatatabledatasetreadxml

C# DataSet.ReadXml() omits some columns


Here is my XML:

  <notebooks>
    <notebook>
      <is-default type="boolean">true</is-default>
      <name>James's notebook</name>
      <id>MS4zfDEvMS9Ob3RlYm9va3wzLjM=</id>
    </notebook>
    <notebook>
      <is-default type="boolean">false</is-default>
      <name>James alternate notebook</name>
      <id>NDQuMnwzNC8zNC9Ob3RlYm9va3wxMTIuMg==</id>
    </notebook>
    <notebook>
      <is-default type="boolean">false</is-default>
      <name>Notebook for James lab</name>
      <id>NTMuM3w0MS80MS9Ob3RlYm9va3wxMzUuMw==</id>
    </notebook>
  </notebooks>

and I am doing the standard DataSet load that I found here:

    DataSet ds = new DataSet();
    ds.ReadXml(new StringReader(lcXMLSegment));

Things work fine, except the "notebook" table only ends up with columns "name" and "id". The column "is-default" does not come through unless I remove the type="boolean" from each element.

I have tried to create a DataTable schema before performing the ReadXml(), but that doesn't do anything, because I assume the DataSet.ReadXml() re-creates the tables inside the data set. I have also tried various second parameters on ReadXml() to read or infer the schema and it makes no difference (or throws errors).

This XML is what is returned from a web API I wish to access and do custom programming against, so I have no control over the native structure of the XML. I am fine if the "is-default" field does not pull in as type boolean, as I can handle all the data being of string type. I am also fine creating full schemas for any data I load in from XML, but I don't know where or how I would do that in this case.


Solution

  • If you really do not need this attribute, it can be removed as follows:

    XElement xml = XElement.Parse(lcXMLSegment);
    // or load from file:  .Load("file.xml");
    
    xml.Elements("notebook")
        .Elements("is-default")
        .Attributes("type")
        .Remove();
    

    Using classes for working with XML like LinqToXml is more appropriate way than by using a string methods like Replace.

    Then reads the data directly from the XElement:

    DataSet ds = new DataSet();
    
    using (var reader = xml.CreateReader())
        ds.ReadXml(reader);