How to compare dictonary key with xml attribute value in xml using LINQ in c #?

Posted by Pramodh on Stack Overflow See other posts from Stack Overflow or by Pramodh
Published on 2010-05-25T04:21:13Z Indexed on 2010/05/25 5:01 UTC
Read the original article Hit count: 247

Filed under:
|
|

Dear all,

i've a dictonary " dictSample " which contains

1 data1
2 data2
3 data3
4 data4

and an xml file"sample.xml" in the form of:

<node>
 <element id="1" value="val1"/>
 <element id="2" value="val2"/>
 <element id="3" value="val3"/>
 <element id="4" value="val4"/>
 <element id="5" value="val5"/>
 <element id="6" value="val6"/>
 <element id="7" value="val7"/>
</node>

i need to match the dictonary keys with the xml attribute id and to insert the matching id and the value of attribute"value" into another dictonary

now i'm using like:

XmlDocument XDOC = new XmlDocument();
XDOC.Load("Sample.xml");
XmlNodeList NodeList = XDOC.SelectNodes("//element");
Dictionary<string, string> dictTwo = new Dictionary<string, string>();
foreach (string l_strIndex in dictSample .Keys)
        {
            foreach (XmlNode XNode in NodeList)
            {
                XmlElement XEle = (XmlElement)XNode;
                if (dictSample[l_strIndex] == XEle.GetAttribute("id"))
                    dictTwo.Add(dictSample[l_strIndex], XEle.GetAttribute("value").ToString());
            }
        }

please help me to do this in a simple way using LINQ

© Stack Overflow or respective owner

Related posts about c#

Related posts about Xml