-
February 7th, 2011, 07:44 AM
#1
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.
-
February 9th, 2011, 09:05 AM
#2
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|