XmlDataDocument.DataSet 属性

定义

获取 DataSet,该数据集提供 XmlDataDocument 中数据的关系表示形式。

public:
 property System::Data::DataSet ^ DataSet { System::Data::DataSet ^ get(); };
public System.Data.DataSet DataSet { get; }
member this.DataSet : System.Data.DataSet
Public ReadOnly Property DataSet As DataSet

属性值

DataSet,该数据集可用于访问使用关系模型的 XmlDataDocument 中的数据。

示例

以下示例使用 DataSet 方法修改书籍的价格。

#using <System.dll>
#using <System.Xml.dll>
#using <System.Data.dll>

using namespace System;
using namespace System::Data;
using namespace System::Xml;

int main()
{
   
   //Create an XmlDataDocument.
   XmlDataDocument^ doc = gcnew XmlDataDocument;
   
   //Load the schema file.
   doc->DataSet->ReadXmlSchema( "store.xsd" );
   
   //Load the XML data.
   doc->Load( "2books.xml" );
   
   //Update the price on the first book using the DataSet methods.
   DataTable^ books = doc->DataSet->Tables[ "book" ];
   books->Rows[ 0 ][ "price" ] = "12.95";
   Console::WriteLine( "Display the modified XML data..." );
   doc->Save( Console::Out );
}
using System;
using System.Data;
using System.Xml;

public class Sample
{
  public static void Main()
  {
     //Create an XmlDataDocument.
     XmlDataDocument doc = new XmlDataDocument();

     //Load the schema file.
     doc.DataSet.ReadXmlSchema("store.xsd");

     //Load the XML data.
     doc.Load("2books.xml");

     //Update the price on the first book using the DataSet methods.
     DataTable books = doc.DataSet.Tables["book"];
     books.Rows[0]["price"] = "12.95";

     Console.WriteLine("Display the modified XML data...");
     doc.Save(Console.Out);
  }
} // End class
imports System.Data
imports System.Xml

public class Sample

  public shared sub Main()

    'Create an XmlDataDocument.
    Dim doc as XmlDataDocument = new XmlDataDocument()

    'Load the schema.
    doc.DataSet.ReadXmlSchema("store.xsd") 
 
    'Load the XML data.
    doc.Load("2books.xml")

    'Change the price on the first book using the DataSet methods.
    Dim books as DataTable = doc.DataSet.Tables.Item("book")
    books.Rows.Item(0).Item("price") = "12.95" 

    Console.WriteLine("Display the modified XML data...")
    doc.Save(Console.Out)

  end sub
end class

该示例使用以下两个文件作为输入。

2books.xml


<!--sample XML fragment-->
<bookstore>
  <book genre='novel' ISBN='10-861003-324'>
    <title>The Handmaid's Tale</title>
    <price>19.95</price>
  </book>
  <book genre='novel' ISBN='1-861001-57-5'>
    <title>Pride And Prejudice</title>
    <price>24.95</price>
  </book>
</bookstore>

store.xsd


<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

 <xsd:element name="bookstore" type="bookstoreType"/>

 <xsd:complexType name="bookstoreType">
  <xsd:sequence maxOccurs="unbounded">
   <xsd:element name="book"  type="bookType"/>
  </xsd:sequence>
 </xsd:complexType>

 <xsd:complexType name="bookType">
  <xsd:sequence>
   <xsd:element name="title" type="xsd:string"/>
   <xsd:element name="author" type="authorName"/>
   <xsd:element name="price"  type="xsd:decimal"/>
  </xsd:sequence>
  <xsd:attribute name="genre" type="xsd:string"/>
 </xsd:complexType>

 <xsd:complexType name="authorName">
  <xsd:sequence>
   <xsd:element name="first-name"  type="xsd:string"/>
   <xsd:element name="last-name" type="xsd:string"/>
  </xsd:sequence>
 </xsd:complexType>

</xsd:schema>

注解

DataSet使你能够使用关系模型访问 中的数据XmlDataDocument。 这意味着可以将数据作为表和视图、行和列、关系等进行处理。 中 DataSet 所做的更改在 中 XmlDataDocument立即可见。

适用于

另请参阅