I have the following XML document named testFix.fix
<WriteFixedWidth Type="extract">
<Position Start="1" Length="15" Name="Field1" />
<Position Start="16" Length="8" Name="Field2" />
<Position Start="24" Length="10" Name="Field3" />
</WriteFixedWidth>
Also, the following code
public void readXML()
{
XDocument loaded = XDocument.Load(@"testSpec.xml");
var q = from c in loaded.Descendants("WriteFixedWidth").Elements("Position")
where c.Parent.Attribute("Type").ToString() == "Extract"
select new
{
Start = c.Attribute("Start").Value,
Length = c.Attribute("Length").Value,
Name = c.Attribute("Name").Value
};
foreach (var field in q)
Console.WriteLine("Name is {0}, Start is {1}, Length is {2}", field.Name, field.Start, field.Length);
}
If I remove the where clause I get all of the fields in this XML document as expected. However I would have different “Type” attributes for different operations. How do I filter the data from the parent node? It would be nice to see this as one query rather than building two.
When accessing an attribute, you need to use the
Valueproperty, rather thanToString().(Also note that “extract” is lowercase in your sample, but uppercase in your query)