364,088 Members | 5420 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

LINQ TO XML get node based on multiple conditions

adnan jan
P: 6
How can i get the node FieldName based on xID && dID through LINQ TO XML.
Note: dID may or may not exist.
Expand|Select|Wrap|Line Numbers
  1.     <Configuration>
  2.         <Contract xID="2">
  3.             <Document dID="227">
  4.                 <FieldName name="AAAA"/>
  5.                 <FieldName name="BBBB"/>
  6.             </Document>
  7.         </Contract>
  8.         <Contract xID="5">
  9.  
  10.                 <FieldName nam`enter code here`e="CCCC"/>
  11.                 <FieldName name="DDDD"/>
  12.  
  13.         </Contract>
  14.     </Configuration>
  15.  
What i had tried so far is printed below. However my code does not handle the condition when dID doesn not exist
Expand|Select|Wrap|Line Numbers
  1.     XDocument xmlDoc = XDocument.Load("../../FieldConfiguration.xml");
  2.  
  3.                 var fieldNames = (from n in xmlDoc.Descendants("Contract")
  4.                                   where (int)n.Attribute("xID") == 5 &&
  5.                                            (int)n.Element("Document").Attribute("dID") == 227
  6.                                   from f in n.Element("Document").Elements()
  7.                                   select (string) f.Attribute("name")).ToList();
  8.  
  9.                 foreach (string name in fieldNames)
  10.                 {
  11.                     Console.WriteLine("Site: " + name);
  12.                 }
  13.  
Jan 22 '12 #1
Share this Question
Share on Google+
2 Replies


adnan jan
P: 6
Any help will be highly appreciated...
Jan 22 '12 #2

Joseph Martell
Expert 100+
P: 165
As a general tip, I would say that I find it helpful to start these types of queries with the element that you are trying to extract data from i.e., instead of selecting the contract and working your way down, start with the FieldName element and use the .Parent property to filter your set of possibilities:

Expand|Select|Wrap|Line Numbers
  1. var fieldNames2 = (from f in xmlDoc.Descendants("FieldName")
  2.                    where f.Parent.Name == "stuff"
  3.                      and (int)f.Parent.Attribute("xID") == 32
  4.                     //more predicate statements
  5.                     select f.Attribute("name")).ToList()
  6.  
  7.  
The code can get quite complex depending on how many cases you have to take care of and how many levels up the XML document you have to go to do your filtering, but it should at least get you on the right track.

If you need more help, then please clarify your question. Your post says that dID may not exist, but your XML has the entire document element missing from the second Contract element. Do you have to account for the case where the Document element exists but does not have a dID attribute?

To cover all the bases, what would be the expected result set from the following example XML:

Expand|Select|Wrap|Line Numbers
  1.         <Configuration>
  2.             <Contract xID="2">
  3.                 <Document dID="227">
  4.                     <FieldName name="AAAA"/>
  5.                     <FieldName name="BBBB"/>
  6.                 </Document>
  7.             </Contract>
  8.             <Contract xID="5">
  9.                     <FieldName name="CCCC"/>
  10.                     <FieldName name="DDDD"/>
  11.             </Contract>
  12.             <Contract xID="5">
  13.                 <Document>
  14.                     <FieldName name="EEEE"/>
  15.                     <FieldName name="FFFF"/>
  16.                 </Document>
  17.             </Contract>
  18.             <Contract xID="5">
  19.                 <Document dID="227">
  20.                     <FieldName name="GGGG"/>
  21.                     <FieldName name="HHHH"/>
  22.                 </Document>
  23.             </Contract>
  24.             <Contract xID="5" dID="227">
  25.                     <FieldName name="IIII"/>
  26.                     <FieldName name="JJJJ"/>
  27.             </Contract>
  28.         </Configuration>
  29.  
Jan 23 '12 #3

Post your reply

Help answer this question



Didn't find the answer to your .NET Framework question?

You can also browse similar questions: .NET Framework asp.net c# linq.xml