How can i get the node FieldName based on xID && dID through LINQ TO XML.
Note: dID may or may not exist.
What i had tried so far is printed below. However my code does not handle the condition when dID doesn not exist
Note: dID may or may not exist.
Code:
<Configuration>
<Contract xID="2">
<Document dID="227">
<FieldName name="AAAA"/>
<FieldName name="BBBB"/>
</Document>
</Contract>
<Contract xID="5">
<FieldName nam`enter code here`e="CCCC"/>
<FieldName name="DDDD"/>
</Contract>
</Configuration>
Code:
XDocument xmlDoc = XDocument.Load("../../FieldConfiguration.xml");
var fieldNames = (from n in xmlDoc.Descendants("Contract")
where (int)n.Attribute("xID") == 5 &&
(int)n.Element("Document").Attribute("dID") == 227
from f in n.Element("Document").Elements()
select (string) f.Attribute("name")).ToList();
foreach (string name in fieldNames)
{
Console.WriteLine("Site: " + name);
}
Comment