Geek
Geek

Reputation: 195

How to load xml into a dataset in C#

private const String FormatOverrideCols = 
  "<XmlDS><table1><col1>Value1</col1></table1><table1><col1>Value2</col1></table1></XmlDS>";

System.IO.StringReader xmlSR = new System.IO.StringReader(FormatOverrideCols);
ds.ReadXml(xmlSR, XmlReadMode.IgnoreSchema);

Why does this code snippet not load the data in the xml string into the dataset ??

Upvotes: 0

Views: 1864

Answers (1)

Anthony Pegram
Anthony Pegram

Reputation: 126982

Note the documentation on XmlReadMode.IgnoreSchema

Ignores any inline schema and loads the data into the existing DataSet schema. Any data that does not match the existing schema is discarded. If no schema exists in the DataSet, no data is loaded.

If you are going to ignore the schema, you need to match the existing dataset schema. Alter (or omit) your read mode if your goal is to use the schema from the XML directly.

using (StringReader xmlSR = new StringReader(FormatOverrideCols))
{       
     ds.ReadXml(xmlSR); 
}

Upvotes: 1

Related Questions