Reputation: 5203
I have an XML of the following format. What is the best way to fill the data to Dataset?
<VariableTable>
<InternalName Old="sujith" New="sujitha" />
<InternalName Old="renjith" New="renjitha" />
</VariableTable>
Upvotes: 2
Views: 6278
Reputation: 14
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
Upvotes: -1
Reputation: 15851
Dataset.ReadXML() method, you can use it , and also check for this links Reading xml in Dataset
there are many options.
string xmlFilename = "XmlFilePath.xml"; DataSet yourDataset = new DataSet(); yourDataset.ReadXml(xmlFilename); // do with your filled Dataset
Upvotes: 4
Reputation: 2075
If you can alter the XML a little bit, then DataSets provide built in functionality to load and save data to/from xml.
You could then use methods WriteXml() ReadXml()
If you need to load custom xml file format, then you need to write the code to parse it and populate dataset. For that you could use Linq or XPath.
Hope that helps.
UPDATED
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);
the above code is what I used with WriteToXml method. No this is the output XML format.
<?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>
That's how the data would be stored in XML...
It might be a bit of an overhead if you need something simple, but this is the functionality out of the box.
Does that give you an idea how to do it?
Upvotes: 0