CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2011
    Location
    Netherlands
    Posts
    45

    How to read labels and data from a xml file and store them as variables

    Dear Friends,

    It would be nice if you can help me with this, I checked the net but must of tutorials are so complicated for a beginner like me

    I have created a xml file like this:

    <?xml version="1.0" encoding="utf-8" ?>
    <config>
    <recalls>
    <hbm400neg>D:\setups\hbm400neg.css</hbm400neg>
    <hbm400pos>D:\setups\hbm400neg.css</hbm400pos>
    <hbm1000neg>D:\setups\hbm400neg.css</hbm1000neg>
    <hbm1000pos>D:\setups\hbm400neg.css</hbm1000pos>
    </recalls>
    </config>


    What I want to do is have those 4 sub elemtns of <recalls> to be stored as a string like this for example if we do it normally in the c#:

    string hbm400neg = "D:\setups\hbm400neg.css";

    how is it possible?
    Thanks.

  2. #2
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: How to read labels and data from a xml file and store them as variables

    Code:
    DataSet ds = new DataSet();
    ds.ReadXml(PATH_TO_FILE_OR_OTHER_XML_SOURCE_GOES_HERE);
    foreach(DataTable dt in ds)
      foreach(DataColumn dc in dt.Columns){
       MessageBox.Show(dc.ColumnName);
       foreach(DataRow dr in dt.Rows)
        MessageBox.Show(dr[dc].ToString());
     }
    The only way though to make your program have a variable called hbm400neg is to write code that writes a c# file:

    Code:
    StringBuilder sb = new StringBuilder();
    foreach(DataTable dt in ds)
      foreach(DataColumn dc in dt.Columns){
       sb.AppendFormat("string {0} = \"{1}\";", dc.ColumnName, dt[0][dc].ToString());
     }
    You cannot make variables named this in a running program because, well.. how would you refer to them or use them? The only thing you can do is write a program that writes a C# code file then compile it..

    But then you wouldnt even bother with that.. just use a text editor that supports regular expressions:

    find: \s+[<](\w+)[>].*?[<]/\1[>]
    replace: string \1 = "\2";
    Last edited by cjard; February 9th, 2011 at 09:11 AM.
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured