DataSet ADO.NET 提供數據的關係型表示法。 針對階層式數據存取,您可以使用 .NET Framework 中提供的 XML 類別。 在過去,這兩個數據表示法已分開使用。 然而,.NET Framework 允許透過 DataSet 物件與 XmlDataDocument 物件,即時同步存取關聯式與階層式資料表示。
當 a DataSet 與 XmlDataDocument 同步時,兩個物件都處理同一組資料。 這表示如果對 DataSet 進行變更,變更將會反映在 XmlDataDocument 中,反之亦然。 與 之間的DataSet關係XmlDataDocument創造了極大的彈性,允許單一應用程式使用同一組資料,存取圍繞 的DataSet整套服務(如 Web 表單與 Windows 表單控制項,以及 Visual Studio .NET 設計器),以及包括可擴充樣式表語言(XSL)在內的 XML 服務套件, XSL 轉換(XSLT)以及 XML 路徑語言(XPath)。 您不需要選擇要以應用程式為目標的一組服務;這兩者皆可供使用。
有幾種方法可以將 a DataSet 與 XmlDataDocument 同步。 您可以:
先用結構(即關聯結構)和資料填充 A
DataSet,然後與新的 XmlDataDocument 同步。 這會提供現有關係型數據的階層式檢視。 例如:Dim dataSet As DataSet = New DataSet ' Add code here to populate the DataSet with schema and data. Dim xmlDoc As XmlDataDocument = New XmlDataDocument(dataSet)DataSet dataSet = new DataSet(); // Add code here to populate the DataSet with schema and data. XmlDataDocument xmlDoc = new XmlDataDocument(dataSet);只用結構(一種強型別的 DataSet)填充
DataSet,與 XmlDataDocument 同步,然後從 XML 文件載入XmlDataDocument。 這會提供現有階層式數據的關係型檢視。 你的DataSet結構中資料表名稱和欄位名稱必須與你希望同步的 XML 元素名稱相符。 此比對會區分大小寫。請注意,這個
DataSet結構只需要與你想在關聯式視圖中暴露的 XML 元素相符。 如此一來,您可以在該文件上擁有非常大的 XML 檔和非常小型的關係型「視窗」。 即使DataSet只暴露了一小部分,XmlDataDocument也能保留整個 XML 文件。 (如需此範例的詳細範例,請參閱 使用 XmlDataDocument 同步處理數據集。)以下程式碼範例展示了建立 a
DataSet並填充其結構,然後與 XmlDataDocument 同步的步驟。 請注意,DataSet只需要匹配你想透過DataSet來公開的XmlDataDocument。Dim dataSet As DataSet = New DataSet ' Add code here to populate the DataSet with schema, but not data. Dim xmlDoc As XmlDataDocument = New XmlDataDocument(dataSet) xmlDoc.Load("XMLDocument.xml")DataSet dataSet = new DataSet(); // Add code here to populate the DataSet with schema, but not data. XmlDataDocument xmlDoc = new XmlDataDocument(dataSet); xmlDoc.Load("XMLDocument.xml");如果
XmlDataDocument與包含資料的DataSet同步,你將無法載入。 將會擲回例外狀況。建立一個新
XmlDataDocument檔案並從 XML 文件載入,然後利用DataSetXmlDataDocument 的屬性存取資料的關聯式檢視。 你需要先設定DataSet,才能使用DataSet查看XmlDataDocument。 同樣地,你的DataSet結構中資料表名稱和欄位名稱必須與你希望同步的 XML 元素名稱相符。 此比對會區分大小寫。下列程式代碼範例示範如何存取 XmlDataDocument 中數據的關係型檢視。
Dim xmlDoc As XmlDataDocument = New XmlDataDocument Dim dataSet As DataSet = xmlDoc.DataSet ' Add code here to create the schema of the DataSet to view the data. xmlDoc.Load("XMLDocument.xml")XmlDataDocument xmlDoc = new XmlDataDocument(); DataSet dataSet = xmlDoc.DataSet; // Add code here to create the schema of the DataSet to view the data. xmlDoc.Load("XMLDocument.xml");
將 與 XmlDataDocument 同步 DataSet 的另一個優點是 XML 文件的忠實度得以保留。 若 是 DataSet 使用 ReadXML 從 XML 文件填充,當資料以 XML 文件 WriteXml 寫回時,可能會與原始 XML 文件有顯著差異。 這是因為DataSet 不會維持 XML 文件中的格式設定,例如空白或階層資訊,例如元素順序。 此外,也 DataSet 未包含因與 資料集結構不符而被忽略的 XML 文件元素。 與 XmlDataDocument a DataSet 同步後,原始 XML 文件的格式與階層元素結構可在 XmlDataDocument 中保留,而 XML DataSet 僅包含適合 DataSet 的資料與結構資訊。
當 a DataSet 與 XmlDataDocument 同步時,結果可能會因你的 DataRelation 物件是否巢狀而有所不同。 如需詳細資訊,請參閱 巢狀資料關係。
本節中
將 DataSet 與 XmlDataDocument 同步 ,示範如何將一個結構最小的強型別 DataSet 與 XmlDataDocument 同步。
對資料集執行 XPath 查詢 示範對 資料集內容執行 XPath 查詢。
對資料集應用 XSLT 轉換 示範如何對 資料集內容應用 XSLT 轉換。
相關區段
在資料集中使用 XML 描述 該 如何 DataSet 以資料來源與 XML 互動,包括載入並持久化 XML DataSet 內容作為 XML 資料。
巢狀資料關係 討論巢狀 DataRelation 物件在以 XML 資料表示內容 DataSet 時的重要性,並說明如何建立這些關聯。
DataSets、DataTables 與 DataView 描述 DataSet 及其如何管理應用程式資料及與包括關聯式資料庫及 XML 在內的資料來源互動。
XmlDataDocument 包含關於該 XmlDataDocument 類別的參考資訊。
另請參閱
- ADO.NET 概觀