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>
source share