开发者

Create dataset from xmlfile

开发者 https://www.devze.com 2022-12-18 18:55 出处:网络
I have the following code string myXMLfile = @\"path to file\"; DataSet ds = new DataSet(\"myDataset\");

I have the following code

           string myXMLfile = @"path to file";
           DataSet ds = new DataSet("myDataset");
           DataTable dataTable = new DataTable("ExtID");
           dat开发者_开发技巧aTable.Columns.Add("Ext", typeof(string));
           dataTable.Columns.Add("TargetPath", typeof(string));
           ds.Tables.Add(dataTable);
           ds.ReadXml(myXMLfile);

The dataset contains the columns and the amount of rows is correct but they're all empty. What am I doing wrong


Does the XML contain the values for a single table? Or for a full dataset (multiple tables, with relationships between them)?

If it's a single data table: try loading the XML on the data table instead:

dataTable.ReadXml(myXMLfile);

Marc


You don't need to create the DataTable if your file already has the right columns. Just do:

DataSet ds = new DataSet(); 
ds.ReadXml("FilePath");

Once it's loaded you can access the data table at ds.Tables[0];


Here is another solution that reads from XML string and reconstructs DataSet in VB.net

Public Function StringXMLToDataSet(ByVal XMLString As String) As DataSet
    Dim StringReader As New StringReader(XMLString)
    Dim dset As New DataSet()
    dset.ReadXml(StringReader)
    Return dset
End Function
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号