none
How to attach XML to a dataset? RRS feed

  • Question

  • Hi ,

    In my Vb.net project, I am making use of an XML file. But if there arises a condition in which the XML file is not present in the specified location, I should automatically create the same file. I have the following code:

    Private

    Sub Form_XML_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

     

     

    If System.IO.File.Exists(Application.StartupPath & "\XMLName.xml") = True Then

    dsRuleSheet.ReadXml(Application.StartupPath &

    "\XMLName.xml")

     

     

    Else

    MsgBox(

    "XMLName.xml not found", MsgBoxStyle.Critical)

     

     

    Dim strDefaultRule As String = "<?xml version=""" & "1.0" & """standalone=""" & "yes" & """?><dsRuleFile xmlns=""" & "http://tempuri.org/dsRuleFile.xsd" & """> <File>    <Name>A</Name>    <ID>100</ID>    <Address>ABC</Address>  </File>  <File>    <Name>B</Name>    <ID>200</ID>    <Address>CDE</Address>  </File>  <File>    <Name>C</Name>    <ID>300</ID>    <Address>EFG</Address>  </File>  <File>    <Name>D</Name>    <ID>400</ID>    <Address>DER</Address>  </File></dsFile>"

     

    End If

    dgRuleGrid.DataSource = dsRuleSheet.Tables(0)

     

    End Sub

     

    How will I be able to use the string variable to create the XML and associate it with a DataSource? Please help.

     "I want the code here.....

     

    Friday, March 19, 2010 9:12 AM

Answers

  • Val's suggestion is the way I would do it to. Create a DataTable and add the columns manually (unless your dsRuleSheet DataSet already has the DataTable in it):

    dsRuleSheet.Tables.Add(new DataTable());
    dsRuleSheet.Tables[0].Columns.Add("Name"); // typeof(string) is default
    dsRuleSheet.Tables[0].Columns.Add("ID");
    dsRuleSheet.Tables[0].Columns.Add("Address");

    Then, add rows to your Table manually and output the XML file where you need to:

    DataRow row = dsRuleSheet.Tables[0].NewRow();
    row["Name"] = "A";
    row["ID"] = "100";
    row["Address"] = "ABC";
    dsRuleSheet.Tables[0].Rows.Add(row);
     
    // next row
    row = dsRuleSheet.Tables[0].NewRow();
    //etc.etc.
     
    // When you're done, write out the file:
    dsRuleSheet.WriteXml(Application.StartupPath + \\XMLName.xml);

    Sorry for the C# code, I didn't notice you were using VB, but it's roughly the same (I think you use parens instead of brackets is the major difference).

    Anyway, I think this is easier and less error-prone than writing out that entire XML string by hand!


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Saturday, March 20, 2010 11:46 PM

All replies

  • To me it looks like you need to create DatatTable on a fly. If this is correct, then you could create columns in yout DataTable adding them to Columns collection instead of creating XML string representation of the DataTable.


    Val Mazur (MVP) http://www.xporttools.net
    Friday, March 19, 2010 10:03 AM
    Moderator
  • Hi,

    What I need is an XML(the data would contain around 100 rows) which should be automatically created and placed at Application.StartupPath with the name XMLName.xml

    Could you provide more information on this?

    Friday, March 19, 2010 10:19 AM
  • Val's suggestion is the way I would do it to. Create a DataTable and add the columns manually (unless your dsRuleSheet DataSet already has the DataTable in it):

    dsRuleSheet.Tables.Add(new DataTable());
    dsRuleSheet.Tables[0].Columns.Add("Name"); // typeof(string) is default
    dsRuleSheet.Tables[0].Columns.Add("ID");
    dsRuleSheet.Tables[0].Columns.Add("Address");

    Then, add rows to your Table manually and output the XML file where you need to:

    DataRow row = dsRuleSheet.Tables[0].NewRow();
    row["Name"] = "A";
    row["ID"] = "100";
    row["Address"] = "ABC";
    dsRuleSheet.Tables[0].Rows.Add(row);
     
    // next row
    row = dsRuleSheet.Tables[0].NewRow();
    //etc.etc.
     
    // When you're done, write out the file:
    dsRuleSheet.WriteXml(Application.StartupPath + \\XMLName.xml);

    Sorry for the C# code, I didn't notice you were using VB, but it's roughly the same (I think you use parens instead of brackets is the major difference).

    Anyway, I think this is easier and less error-prone than writing out that entire XML string by hand!


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Saturday, March 20, 2010 11:46 PM
  • Hi, Thank you all for your answers.
    Monday, March 22, 2010 5:09 AM
  • Val's suggestion is the way I would do it to. Create a DataTable and add the columns manually (unless your dsRuleSheet DataSet already has the DataTable in it):

    dsRuleSheet.Tables.Add(new DataTable());
    dsRuleSheet.Tables[0].Columns.Add("Name"); // typeof(string) is default
    dsRuleSheet.Tables[0].Columns.Add("ID");
    dsRuleSheet.Tables[0].Columns.Add("Address");

    Then, add rows to your Table manually and output the XML file where you need to:

    DataRow row = dsRuleSheet.Tables[0].NewRow();
    row["Name"] = "A";
    row["ID"] = "100";
    row["Address"] = "ABC";
    dsRuleSheet.Tables[0].Rows.Add(row);
     
    // next row
    row = dsRuleSheet.Tables[0].NewRow();
    //etc.etc.
     
    // When you're done, write out the file:
    dsRuleSheet.WriteXml(Application.StartupPath + \\XMLName.xml);

    Sorry for the C# code, I didn't notice you were using VB, but it's roughly the same (I think you use parens instead of brackets is the major difference).

    Anyway, I think this is easier and less error-prone than writing out that entire XML string by hand!


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com

    It has been solved, but why's that? Your effort is appreciated! Would you pls offer the related documentation?
    Friday, October 8, 2010 3:14 AM
  • Dinah,

    Which document do you refer to?


    Val Mazur (MVP) http://www.xporttools.net
    Friday, October 8, 2010 10:16 AM
    Moderator