I'm not sure if this is possible but here is what I want to do: I have created a stro开发者_Go百科ngly-typed dataset, StrTypDS
using the dataset designer in Visual Studio. I have also created an blank xml file and added it to my project as a Resource so that it can be accessed through Properties.Resources.xmlData. The what I would like to do is read the xml file and see if any data matches that of the schema of my strongly typed dataset. If there is matching data, I want to read it into the dataset to that I can process it. If there isn't matching data, I want to write the schema to the xml file so that I can store the strongly typed data there for future reads. What is the simplest way to do this?
You can't write to a resource, because the resource file is compiled into the assembly.
To try to read xml files into your strongly typed dataset, use this example (VB.NET)...
I have created two typed datasets, ds1 and ds2 with different schemas. I write out two sample xml data files without their schemas. I then try to read every xml file in a folder to find the xml file that matches my target strongly typed dataset. If my target dataset has data in it after a read attempt, the schema was a match.
Private Const InputFolder As String = "C:\xmlinputdata\"
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
WriteTestXML()
For Each s In IO.Directory.GetFiles(InputFolder, "*.xml")
Dim dsTarget As New ds1
dsTarget.ReadXml(s, Data.XmlReadMode.IgnoreSchema)
If DatasetHasData(dsTarget) Then
MsgBox(dsTarget.GetXml)
End If
Next
End Sub
Private Function DatasetHasData(ByVal ds As Data.DataSet) As Boolean
For Each dt As Data.DataTable In ds.Tables
If dt.Rows.Count > 0 Then
Return True
End If
Next
Return False
End Function
Private Sub WriteTestXML()
Dim x As New ds1
x.dt1.Adddt1Row("A", "B")
x.dt1.Adddt1Row("C", "D")
x.WriteXml(InputFolder & "ds1.xml", Data.XmlWriteMode.IgnoreSchema)
Dim y As New ds2
With y.dt2
.Adddt2Row(1, 2, 3)
.Adddt2Row(4, 5, 6)
End With
y.WriteXml(InputFolder & "ds2.xml", Data.XmlWriteMode.IgnoreSchema)
End Sub
精彩评论