Get "Key" attribute in XML config file



I have an XML file with a structure and trying to get value from "Key" and "Value":



<appSettings>
<add key="Url" value=" http://ift.tt/YK2TsP"/>

<!--Id's or Xpath-->
<add key="Origin" value="somevalue"/>
<add key="Destination" value="somevalue"/>
<add key="Adult" value ="somevalue" />
<add key="somevalue"/>

<!--Controls-->
<add key="OriginCtrl" value=" Input"/>
<!--Textbox-->
<add key="DestinationCtrl" value=" Input"/>
<add key="AdultCtrl" value=" Select"/>
<add key="SearchFlightsCtrl " value=" Button"/>

</appSettings>


I am trying to create a loop that loops through the XML and get value of these "key" and "value". The code I am writing is



XmlDocument xmlDoc = new XmlDocument();
XmlNodeList xmlnodelist;
string keyname = "";
string keyvalue = "";
xmlDoc.Load(filename);
xmlnodelist = xmlDoc.SelectNodes("appSettings");

foreach (XmlNode nodes in xmlnodelist)
{
keyname = nodes.Attributes.GetNamedItem("key").Value;
keyvalue = nodes.Attributes.GetNamedItem("value").Value;
}


The error with "object not set to an instance" rises when i try to run this piece of code. I wonder if there is errors in the code where getting the Value. Any advices would be appreciated.


No comments:

Post a Comment