如何在引用数据集中的表时指定命名空间

时间:2022-02-13 15:38:55

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?

我正在使用ReadXml方法从XML文件将数据加载到DataSet中。这导致两个具有相同名称的表。其中一个表有命名空间而另一个没有。我正在尝试使用命名空间引用该表。谁能告诉我怎么做?

    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()

2 个解决方案

#1


0  

From MSDN: DataSet.Namespace

从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.

使用ReadXml,WriteXml,ReadXmlSchema或WriteXmlSchema方法在XMLSet中读取和写入XML文档时使用Namespace属性。

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.

XML文档的名称空间用于在读入DataSet时对XML属性和元素进行范围调整。例如,如果DataSet包含从具有命名空间“myCompany”的文档中读取的模式,并且尝试仅从具有不同命名空间的文档读取数据,则任何与现有模式不对应的数据都是忽略。

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

以下示例在调用ReadXml方法之前设置前缀。

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.

我从您给出的示例中看不到,但除非您在加载前设置前缀,否则您将无法读取与现有架构不对应的数据。

#2


0  

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.

我找到了答案。您可以将名称空间作为第二个参数传递。我想我没有注意到Intellisense中的这种特殊过载。

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

#1


0  

From MSDN: DataSet.Namespace

从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.

使用ReadXml,WriteXml,ReadXmlSchema或WriteXmlSchema方法在XMLSet中读取和写入XML文档时使用Namespace属性。

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.

XML文档的名称空间用于在读入DataSet时对XML属性和元素进行范围调整。例如,如果DataSet包含从具有命名空间“myCompany”的文档中读取的模式,并且尝试仅从具有不同命名空间的文档读取数据,则任何与现有模式不对应的数据都是忽略。

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

以下示例在调用ReadXml方法之前设置前缀。

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.

我从您给出的示例中看不到,但除非您在加载前设置前缀,否则您将无法读取与现有架构不对应的数据。

#2


0  

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.

我找到了答案。您可以将名称空间作为第二个参数传递。我想我没有注意到Intellisense中的这种特殊过载。

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