How to read XML in a DataTable when XML is in a specific namespace

I want to read some XML data in a DataTable - using a ReadXml method like

  var dataTable = new DataTable(); XmlReader xmlReader = XmlReader.Create(new StringReader(xmlString)); dataTable.ReadXml(xmlReader); 

See below XML line. XML includes the definition of a schema, and everything is fine and dandy when the data does not have a namespace (i.e. is in the global namespace), but I can not determine how to specify the schema and XML so that the XML data elements are in a different namespace.

The exception I get is the β€œDataTable” cd: Motorcycles' does not match any DataTable in the source. "

I know that I am making some really stupid mistake for a schoolboy, but now I pull my hair for an hour or so - messing around without success.

Maybe someone pulled me out of my misery?

XML that works

  <NewDataSet xmlns=""> <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="Motorcycles" msdata:UseCurrentLocale="true"> <xs:complexType> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element name="Motorcycles"> <xs:complexType> <xs:sequence> <xs:element name="Manufacturer" type="xs:string" minOccurs="0" /> <xs:element name="PercentageOfRiders" type="xs:int" minOccurs="0" /> </xs:sequence> </xs:complexType> </xs:element> </xs:choice> </xs:complexType> </xs:element> </xs:schema> <Motorcycles> <Manufacturer>Honda</Manufacturer> <PercentageOfRiders>23</PercentageOfRiders> </Motorcycles> <Motorcycles> <Manufacturer>Yamaha</Manufacturer> <PercentageOfRiders>15</PercentageOfRiders> </Motorcycles> <Motorcycles> <Manufacturer>Suzuki</Manufacturer> <PercentageOfRiders>16</PercentageOfRiders> </Motorcycles> <Motorcycles> <Manufacturer>BMW</Manufacturer> <PercentageOfRiders>6</PercentageOfRiders> </Motorcycles> <Motorcycles> <Manufacturer>Other</Manufacturer> <PercentageOfRiders>40</PercentageOfRiders> </Motorcycles> </NewDataSet> 

Namespace XML (not working)

  <cd:NewDataSet xmlns="urn:ChartData" xmlns:cd="urn:ChartData"> <xs:schema id="NewDataSet" targetNamespace="urn:ChartData" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" elementFormDefault="qualified"> <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="cd:Motorcycles" msdata:UseCurrentLocale="true"> <xs:complexType> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element name="Motorcycles"> <xs:complexType> <xs:sequence> <xs:element name="Manufacturer" type="xs:string" minOccurs="0" /> <xs:element name="PercentageOfRiders" type="xs:int" minOccurs="0" /> </xs:sequence> </xs:complexType> </xs:element> </xs:choice> </xs:complexType> </xs:element> </xs:schema> <cd:Motorcycles> <cd:Manufacturer>Honda</cd:Manufacturer> <cd:PercentageOfRiders>23</cd:PercentageOfRiders> </cd:Motorcycles> <cd:Motorcycles> <cd:Manufacturer>Yamaha</cd:Manufacturer> <cd:PercentageOfRiders>15</cd:PercentageOfRiders> </cd:Motorcycles> <cd:Motorcycles> <cd:Manufacturer>Suzuki</cd:Manufacturer> <cd:PercentageOfRiders>16</cd:PercentageOfRiders> </cd:Motorcycles> <cd:Motorcycles> <cd:Manufacturer>BMW</cd:Manufacturer> <cd:PercentageOfRiders>6</cd:PercentageOfRiders> </cd:Motorcycles> <cd:Motorcycles> <cd:Manufacturer>Other</cd:Manufacturer> <cd:PercentageOfRiders>40</cd:PercentageOfRiders> </cd:Motorcycles> </cd:NewDataSet> 
+4
source share
1 answer

There are two things to consider when reading this XML:

  • XML urn:ChartData namespace urn:ChartData
  • name of the main data table inside the data set - you will find it here:

     <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="cd:Motorcycles" <== this name here is important msdata:UseCurrentLocale="true"> 

With these two things in mind, you need to change the way you instantiate the DataTable , and then reading is just fine:

 // define the data table name to be the value of the msdata:MainDataTable // attribute on the NewDataSet element, and define the XML namespace to use DataTable dataTable = new DataTable("Motorcycles", "urn:ChartData"); XmlReader xmlReader = XmlReader.Create(new StringReader(xmlString)); dataTable.ReadXml(xmlReader); 

Your DataTable should now contain some entries in the .Rows property - five in my case (with a second XML example).

+10
source

Source: https://habr.com/ru/post/1315673/


All Articles