1

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
sutekh137
  • 681
  • 1
  • 6
  • 18
  • In this case DataSet.ReadXml returns 2 tables. Tables[0] with columns `name` and `id`. And Tables[1] with columns `type` and `is-default_Text`. – Alexander Petrov Aug 24 '15 at 22:46
  • Thank you! That helps! I have taken to simply removing any "extraneous" attributes out of the tags using Replace(). Any way to tell ReadXml() to ignore that stuff? I suppose I could add columns back into table 0 using the data from table 1, but that sounds like a pain. Probably something I should learn, though. – sutekh137 Aug 26 '15 at 16:27

2 Answers2

2

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);
Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49
  • Thank you, Alexander. I will be trying to use more LINQ in the future. Right now I am just knocking this app out. But I've learned more from you than all my messing around so far. *smile* – sutekh137 Aug 28 '15 at 13:15
0

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));
Rhys Jones
  • 5,348
  • 1
  • 23
  • 44