sutekh137
sutekh137

Reputation: 691

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.

Upvotes: 1

Views: 2428

Answers (2)

Rhys Jones
Rhys Jones

Reputation: 5508

Another approach to this problem is to prepare the datatable in advance;

DataSet ds = new DataSet();
DataTable dt = new DataTable("notebook");
dt.Columns.Add("is-default", typeof(bool));
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("id", typeof(string));
ds.Tables.Add(dt);

ds.ReadXml(new StringReader(lcXMLSegment));

Upvotes: 0

Alexander Petrov
Alexander Petrov

Reputation: 14231

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);

Upvotes: 2

Related Questions