正在将XML数据填充到数据集
本文关键字:填充 数据集 数据 XML | 更新日期: 2023-09-27 18:24:36
我有一个以下格式的XML。将数据填充到数据集的最佳方式是什么?
<VariableTable>
<InternalName Old="sujith" New="sujitha" />
<InternalName Old="renjith" New="renjitha" />
</VariableTable>
Dataset.ReadXML()方法,您可以使用它,还可以检查Dataset 中Reading xml的链接
有很多选择。
- 提供xml文件路径,然后将其加载到数据集
- 使用XmlTextReader将其传递给Dataset readxml方法
string xmlFilename = "XmlFilePath.xml"; DataSet yourDataset = new DataSet(); yourDataset.ReadXml(xmlFilename); // do with your filled Dataset
如果您可以稍微更改XML,那么DataSets提供了将数据加载到XML或从XML保存数据的内置功能。
然后你可以使用方法WriteXml()ReadXml()
如果您需要加载自定义的xml文件格式,那么您需要编写代码来解析它并填充数据集。为此,您可以使用Linq或XPath。
希望能有所帮助。
更新
var dataSet = new DataSet("TestDataSet");
var dataTable = new DataTable("TestTable");
dataTable.Columns.Add("ID", typeof(Int32));
dataTable.Columns.Add("Value", typeof(string));
dataTable.Rows.Add(1, "Value1");
dataSet.Tables.Add(dataTable);
dataSet.WriteXml(@"L:'ds.xml", XmlWriteMode.WriteSchema);
上面的代码是我在WriteToXml方法中使用的代码。不,这是输出XML格式。
<?xml version="1.0" standalone="yes"?>
<TestDataSet>
<xs:schema id="TestDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="TestDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="TestTable">
<xs:complexType>
<xs:sequence>
<xs:element name="ID" type="xs:int" minOccurs="0" />
<xs:element name="Value" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<TestTable>
<ID>1</ID>
<Value>Value1</Value>
</TestTable>
</TestDataSet>
这就是数据存储在XML中的方式。。。
如果你需要一些简单的东西,这可能会有点开销,但这是开箱即用的功能。
这能让你知道怎么做吗?
Public Function FillDataSetFromXML(ByVal Procedure As String, ByVal param As List(Of SqlParameter)) As DataSet
Dim dbcon As New dbConnection()
Dim cmd As New SqlCommand()
Dim ds As New DataSet()
Dim XR As XmlReader
XR = Nothing
Try
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = Procedure
dbcon.OpenConnection()
cmd.Connection = dbcon.GetDbConnection()
If param.Count > 0 Then
For Each p As SqlParameter In param
cmd.Parameters.AddWithValue(p.ParameterName, p.Value)
Next
End If
XR = cmd.ExecuteXmlReader()
ds.ReadXml(XR)
Return ds
Catch ex As Exception
Return ds
Finally
dbcon.CloseConnection()
End Try
End Function
Public Function ExecuteScalar(ByVal Qry As String)
Dim dbcon As New dbConnection
Dim cmd As New SqlCommand
Dim res As String
Try
cmd.CommandType = CommandType.Text
cmd.CommandText = Qry
dbcon.OpenConnection()
cmd.Connection = dbcon.GetDbConnection()
res = cmd.ExecuteScalar().ToString()
Return res
Catch ex As Exception
Return res
Finally
dbcon.CloseConnection()
End Try
End Function
Public Function ExecuteProdedures(ByVal param As List(Of SqlParameter), ByVal Procedure As String) As DataSet
Dim dbcon As New dbConnection
Dim ds As New DataSet()
Try
Dim cmd As New SqlCommand()
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = Procedure
dbcon.OpenConnection()
cmd.Connection = dbcon.GetDbConnection()
If param.Count > 0 Then
For Each p As SqlParameter In param
'cmd.Parameters.AddWithValue(p.ParameterName, p.Value)
cmd.Parameters.Add(p)
Next
End If
Dim XR As XmlReader
XR = cmd.ExecuteXmlReader()
ds.ReadXml(XR)
Return ds
Catch ex As Exception
Finally
dbcon.CloseConnection()
End Try
End Function