hw 2 add in dictionary

Posted by Oswaldlily under C# on 8/21/2012 | Points: 10 | Views : 1238 | Status : [Member] | Replies : 1
<?xml version="1.0" encoding="utf-8" ?>
<Tables>
<Column col1="c1" col2="c2" col3="c3"></validate>
<Column col1="s1" col2="s2" col3="s3"></validate>
</Tables>

call xml file frm codebehind and add in dictionary.
In dictionary key shd be c1 or s1
value=c2 and c3 or s2 and s3




Responses

Posted by: Vasanthmvp on: 8/21/2012 [Member] Starter | Points: 25

Up
0
Down
Hi Oswaldlily,

Here is the solution for you: I'm sure there may be simple way than this, as i am a beginner i tried this way.

ASP Page:
Empty.

CodeBehind:

protected void Page_Load(object sender, EventArgs e)
{

Dictionary<string, string> Items = new Dictionary<string, string>();

DataSet dset = new DataSet();
dset.ReadXml(Server.MapPath("~/DictionaryItems.xml"));

StringBuilder strbkey = new StringBuilder();

foreach (DataRow row in dset.Tables[0].Rows)
{
strbkey.Append(row["col1"]+" ");

}

string x = strbkey.ToString();

string[] keys = x.Split(' ');



StringBuilder strbvalue = new StringBuilder();

foreach (DataRow row in dset.Tables[0].Rows)
{
strbvalue.Append(row["col2"].ToString()+" ");

strbvalue.Append(row["col3"].ToString()+" | ");
}

string y = strbvalue.ToString();

string[] values = y.Split('|');


for (int i = 0; i < values.Count(); i++)
{
Items.Add(keys[i], values[i]);

}


foreach (KeyValuePair<string, string> item in Items)
{
Response.Write(item);
}

}


Your Xml File: DictionaryItems.xml

<Tables>
<Column col1="c1" col2="c2" col3="c3" />
<Column col1="s1" col2="s2" col3="s3" />
</Tables>


Regards,


Awesome Coding !! :)

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response