/ Published in: C#
URL: http://www.aspfree.com/index2.php?option=content&task=view&id=2426&pop=1&hide_ads=1&page=0&hide_js=1
The code is run in LINQPad, which is where the Dump extension method comes from. You can pretend that it just outputs ToString. The input file for this snippet looks like so:
<configuration>
<userSettings>
<HawkConfigGUI.Properties.Settings>
<setting>
<value>testfpga</value>
</setting>
<setting>
<value>test</value>
</setting>
</HawkConfigGUI.Properties.Settings>
</userSettings>
</configuration>
The output of the snippet is
FpgaFilePath => testfpga
FirmwareFilePath => test
So the snippet pulls out an attribute from the 'setting' element and the content (text between tags) of the 'value' sub-element.
Expand |
Embed | Plain Text
var path = @"C:\Users\patk\Desktop\file.xml"; var doc = XDocument.Load(path); //doc.DescendantNodes().Dump(); //doc.XPathSelectElements("//setting").Dump(); //doc.XPathSelectElements("//setting").Attributes().Dump(); //doc.XPathSelectElements("//setting").Descendants().Dump(); var settings = doc.XPathSelectElements("//setting"); foreach (var setting in settings){ var name = setting.Attribute("name").Value; // setting.DescendantNodes().Dump(); var value = setting.XPathSelectElements("value").Single().FirstNode; (name + " => " + value).Dump(); }
Comments
Subscribe to comments
You need to login to post a comment.

XPathSelectElements is in System.Xml.XPath