tags:

views:

544

answers:

2

I'm loading data into a DataSet from an XML file using the ReadXml method. This results in two tables with the same name. One of the tables has a namespace and the other doesn't. I'm trying to reference the table with the namespace. Can anyone tell me how to do this?

    Dim reader As XmlTextReader = New XmlTextReader(strURL)
    Dim city as string = ""
    Dim ds As DataSet = New DataSet()
    ds.Namespace = "HomeAddress"

    ds.ReadXml(reader)        
    city = ds.Tables("Address").Rows(0).Item(2).ToString()
A: 

From MSDN: DataSet.Namespace

The Namespace property is used when reading and writing an XML document into the DataSet using the ReadXml, WriteXml, ReadXmlSchema, or WriteXmlSchema methods.

The namespace of an XML document is used to scope XML attributes and elements when read into a DataSet. For example, if a DataSet contains a schema that was read from a document with the namespace "myCompany," and an attempt is made to read data only from a document with a different namespace, any data that does not correspond to the existing schema is ignored.

The following example sets the Prefix before calling the ReadXml method.

private void ReadData(DataSet thisDataSet)
{
    thisDataSet.Namespace = "CorporationA";
    thisDataSet.Prefix = "DivisionA";

    // Read schema and data.
    string fileName = "CorporationA_Schema.xml";
    thisDataSet.ReadXmlSchema(fileName);
    fileName = "DivisionA_Report.xml";
    thisDataSet.ReadXml(fileName);
}

I cant see from the example you gave, but unless you set your prefix before you load, you wont be able to read data that doesn't correspond to the existing schema.

hearn
So you're saying that a Namespace is intended to be used with a Schema? If I dump an XML file into a dataset, it will automatically create tables based on the XML hierarchy. It just happened that there were two elements with the same name and I get an error when trying to reference them by name.
Kosta
A: 

I found the answer. You can pass in the namespace as the second parameter. I guess I didn't notice this particular overload in Intellisense.

ds.Tables("Address", "HomeAddress").Rows(1).Item(2).ToString()
Kosta