我有以下格式的XML。将数据填充到数据集的最佳方法是什么?
<VariableTable>
<InternalName Old="sujith" New="sujitha" />
<InternalName Old="renjith" New="renjitha" />
</VariableTable>
答案 0 :(得分:4)
Dataset.ReadXML() 方法,您可以使用它,还可以检查此链接Reading xml in Dataset
有很多选择。
string xmlFilename = "XmlFilePath.xml"; DataSet yourDataset = new DataSet(); yourDataset.ReadXml(xmlFilename); // do with your filled Dataset
答案 1 :(得分:0)
如果您可以稍微改变XML,那么DataSet提供了内置功能,可以将数据加载到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中的方式......
如果你需要一些简单的东西,这可能会有点开销,但这是开箱即用的功能。
这会让你知道怎么做吗?
答案 2 :(得分:-1)
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