Return to Snippet

Revision: 21302
at December 9, 2009 16:23 by pckujawa

Initial Code
var path = @"C:\Users\patk\Desktop\file.xml";
var doc = XDocument.Load(path);
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();

Initial URL

Initial Description
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:


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.

Initial Title
Parsing information from an XML source (elements, nodes, attributes) using XPath

Initial Tags

Initial Language