How to read the elements inside nested childs and siblings in a single root using LINQ
My Xml file format
<Config xmlns="urn:Configuration">
<Group name="Model" version="1.01" source="Barium">
<Group tag="MeterSystem" type="option">
<Entry readonly="1" type="string">PW_TP_STANDARD</Entry>
</Group>
<Group tag="Detector" type="option">
<Entry readonly="1" type="string">SD770</Entry>
</Group>
<Group tag="VXtypeOpt" type="option">
<Entry type="string">Vx88</Entry>
</Group>
<Group tag="FlowDirectionOpt" type="option">
<Entry type="string">Upwards</Entry>
</Group>
</Group>
<Group name="Config">
<Entry name="Configuration name" type="string">Default</Entry>
</Group>
My C# Code to read the Xml structure
List<string> fpxmls = System.IO.Directory.GetFiles(@_fpxml_path, "*.xml",
SearchOption.AllDirectories)
.ToList<string>();
foreach (var _file in fpxmls)
{
var MyFPXMLDoc = XDocument.Load(@_file);
var Config = (from config in MyFPXMLDoc .Descendants("Group")
select new
{
source= (string) config.Attribute("source"),
// How should I proceed?
// Using descendants doesnt give any items to Config
// How to extract VxMeterSize= (string)g.(?).(?)// shud return Vx88
});
...
}
The problem is that you've ignored the namespace which is defaulted in Config
. All of the Group
elements are in that namespace, but you're looking for descendantse without a namespace.
You want:
XNamespace ns = "urn:Configuration";
var query = from config in MyFPXMLDoc.Descendants(ns + "Group")
...;
See more on this question at Stackoverflow