I have an XML file with a structure and trying to get value from "Key" and "Value":
<appSettings>
<add key="Url" value=" http://book.jetstar.com/"/>
<!--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.
由于您要获取appSettings
,因此应使用
xmlDoc.SelectNodes("appSettings/add");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.