c# - XDocument Determine Last Child of Parent -


How to decide to run an XML document with XDocument using the descendants that I am on the parent's last child ?

  XDocument xmlDoc = XDocument.Parse (xml) foreach (var element in xmlDoc.Descendants ()) {// Parents should determine the last child and do something here. }   

Example of XML and what is expected to come back, I need to run the document dynamically and XPath or Xquery is not specified because it is not enough for various XML documents Are being used ... just to see if I can do this with XDocument or if a better method still does not specify tag names in XPath or XQuery, unless it is without dynamically Or it can be, without knowing how deep the document can be.

  & lt ;? Xml version =? "1.0" & gt; & Lt; Root & gt; & Lt; Customers & gt; & Lt; Client ID & gt; 1 & lt; / Customer ID & gt; & Lt; Name & gt; Smith, who & lt; / Name & gt; & Lt; CustomerTransDetail & gt; & Lt; CustID & gt; 1 & lt; / CustID & gt; & Lt; CustTransID & gt; 1 & lt; / CustTransID & gt; & Lt; Note & gt; NA & lt; / Note & gt; & Lt; / CustomerTransDetail & gt; & Lt; CustomerTransDetail & gt; & Lt; Client ID & gt; 1 & lt; / Customer ID & gt; & Lt; CustTransID & gt; 2 & lt; / CustTransID & gt; & Lt; Note & gt; N / A & lt; / Note & gt; & Lt; / CustomerTransDetail & gt; & Lt; / Customer & gt; & Lt; Customers & gt; & Lt; Client ID & gt; 2 & lt; / Customer ID & gt; & Lt; Name & gt; Smith, Jane & lt; / Name & gt; & Lt; CustomerTransDetail & gt; & Lt; CustID & gt; 2 & lt; / CustID & gt; & Lt; CustTransID & gt; 1 & lt; / CustTransID & gt; & Lt; Note & gt; N / A & lt; / Note & gt; & Lt; / CustomerTransDetail & gt; & Lt; CustomerTransDetail & gt; & Lt; Client ID & gt; 2 & lt; / Customer ID & gt; & Lt; CustTransID & gt; 2 & lt; / CustTransID & gt; & Lt; Note & gt; N / A & lt; / Note & gt; & Lt; / CustomerTransDetail & gt; & Lt; / Customer & gt; & Lt; / Root & gt;   

I should know that when I hit the parent's last child, then I can do something. So every time I hit the node node or note node, for example i can do something.

I'm still a bit unclear what you are trying to do, but hopefully It is possible you point in the right direction.

You are using XDocument, so I assume that you are at least in .NET 3.5. You can use most of the XML extension methods from Linq to get what you want.

  XDocument doc = XDocument.Parse ("xmlPath"); Foreach (XElement element in doc.Root.Descendants ()) {bool Hadilrine = Element. HSELMents; Bool Hesibbling = Element Elements Bainfishal (). Any () || . Element.ElementsAfterSelf () Any (); Bool isLastChild = Haswallis & amp; Amp; ! Element.ElementsAfterSelf () Any () .; }   

Depending on what you can do when you cross nodes, you may want to consider recurring while walking on that board. In this way you can determine that the elements are siblings, the last brother, etc.

Comments