I am trying to read from a XML file and save information into a Dictionary.
This is how my XML looks like :
<?xml version="1.0" encoding="utf-8" ?>
<testUI_root>
<!--Information for the first coded UI Test-->
<codedUITest name ="CodedUITestMethod1">
<description name ="Entering username from CSV" key = "1"/>
<description name ="Entering password from CSV" key = "2"/>
<description name ="Clicking exit" key ="3"/>
</codedUITest>
</testUI_root>
So I am trying to read and save to a Dictionary, this is how I have tried :
//Load xml
//StringBuilder description = new StringBuilder();
int key;
Dictionary<int, string> valDic = new Dictionary<int, string>();
XDocument xdoc = XDocument.Load("XMLFile1.xml");
//Run query
var lv1s = from lv1 in xdoc.Descendants("codedUITest")
where lv1.Attribute("name").Value.Contains("CodedUITestMethod1")
select new
{
key = lv1.Descendants("key"),
value = lv1.Descendants("name")
};
//loop
foreach (var lv1 in lv1s)
{
foreach (var lv2_k in lv1.key)
{
//it's not entering into this loop
foreach (var lv2_v in lv1.value)
{
// isn't entering here either
key = Convert.ToInt32(lv2_k.Attribute("key").ToString());
valDic.Add(key, lv2_v.Attribute("name").ToString());
}
}
}
foreach (KeyValuePair<int, string> abc in valDic)
{
MessageBox.Show("Value: " + abc.ToString());
}
There is no syntax error, it's a logical one. So this is how my dictionary is supposed to look like.
/*
1.Entering Username from CSV
2.Entering password from CSV
3.Clicking exit
*/
Saved the xml to output directory. ( from vs ) Tried the code in LinqPad, "query is successful" but no output. I barely have any experience with Linq, I apologize in advance if this matter/error was a simple one. Thanks
No comments:
Post a Comment