using System.Collections.Generic;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO.Compression;
You have a three problems here: 1) the xpath query should be `xpath="WellBore/MetaData/Data_Set"` or `xpath="/WELL_SET/WellBore/MetaData/Data_Set"` not `xpath="/WellBore/MetaData/Data_Set"`. The leading `/` means that the xpath is absolute and should start with the root element name - but the root node is named `<WELL_SET>` not `<WellBore>`. 2) To get the text value of the `<Data_Set>` node use `path.InnerText`. 3) `</Metadata>` should be `</MetaData>`. See: https:
const string defaultPrefix = "default";
public static void Test()
foreach (var xpathXml in GetXpathXml())
var xpathDoc = XDocument.Parse(xpathXml);
var dataDoc = XDocument.Parse(GetDataXml());
var nsmgr = new XmlNamespaceManager(new NameTable());
foreach (var attr in root.Attributes().Where(a => a.IsNamespaceDeclaration))
nsmgr.AddNamespace(attr.Name == "xmlns" ? defaultPrefix : attr.Name.LocalName, attr.Value);
Console.WriteLine("\nQuerying data document: ");
Console.WriteLine(dataDoc);
Console.WriteLine("Using Xpath document:");
Console.WriteLine(xpathDoc);
var dt = new DataTable();
foreach (var col in xpathDoc.Descendants("mapping").Select(x => (string)x.Attribute("column")).Distinct())
dt.Columns.Add(col, typeof(string));
foreach (var record in xpathDoc.Descendants("mappings"))
foreach (var field in record.Elements("mapping"))
var xpath = (string)field.Attribute("xpath");
if (!string.IsNullOrEmpty(xpath))
foreach (var path in root.XPathSelectElements(xpath,nsmgr))
Console.WriteLine("Found: {0}", s);
static string GetDataXml() =>
@"<WELL_SET xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns="""">
<Data_Set>TXRRC</Data_Set>
static IEnumerable<string> GetXpathXml() =>
<mapping column=""SOURCE"" xpath=""/WELL_SET/WellBore/MetaData/Data_Set""/>
<mapping column=""SOURCE"" xpath=""WellBore/MetaData/Data_Set""/>
<mapping column=""SOURCE"" xpath=""/WellBore/MetaData/Data_Set""/>
public static class XmlSerializationHelper
public static string GetOuterXml(this XmlNode node, bool indent = true)
using (var textWriter = new StringWriter())
var settings = new XmlWriterSettings
OmitXmlDeclaration = true,
using (var xmlWriter = XmlWriter.Create(textWriter, settings))
return textWriter.ToString();
public static void Main()
Console.WriteLine("Roslyn 2.0 Compiler; Environment version: " + Environment.Version);
Console.WriteLine("Uncaught exception: ");