Select XML Node and Children based on Child Element Value
-
lunedì 6 agosto 2012 15:31
Is there a way to select a parent node (and be able to use the child elements of that node) by the value of a child element of that node?
Example:
<actor> <name>John Doe</name> <id>22</id> <role>driver</role> </actor> <actor> <name>Jane Doe</name> <id>1452</id> <role>passenger</role> </actor> <actor> <name>Jimmy Doe</name> <id>732</id> <role>walker</role> </actor>
I want to select the "actor" with a role of "driver" and be able to then grab that actor's "id".
Thank you!
Tutte le risposte
-
lunedì 6 agosto 2012 16:19
Try like this. Please check the following Example//Load your XML Document or string XDocument xDoc = XDocument.Load("XMLFile1.xml"); var fieldNames = (from n in xDoc.Descendants("actor") where n.Element("role").Value == "driver" select n).ToList(); foreach (var item in fieldNames) { Console.WriteLine("Name--->" + item.Element("name").Value); Console.WriteLine("Id--->" + item.Element("id").Value); Console.WriteLine("Role---> " + item.Element("role").Value); }With Thanks and Regards
Sambath Raj.C
click "Proposed As Answer by" if this post solves your problem or "Vote As Helpful" if a post has been useful to you
Happy Programming!- Proposto come risposta Fernando Soto - MCSDMicrosoft Community Contributor lunedì 6 agosto 2012 16:38
- Contrassegnato come risposta Jason Dot WangMicrosoft Contingent Staff, Moderator martedì 14 agosto 2012 06:43
-
lunedì 6 agosto 2012 16:49
1st of all: your xml file is incorrect. You cannot have multiple root elements (there can be only one). So change the content of the file to:
<actors> <actor> <name>John Doe</name> <id>22</id> <role>driver</role> </actor> <actor> <name>Jane Doe</name> <id>1452</id> <role>passenger</role> </actor> <actor> <name>Jimmy Doe</name> <id>732</id> <role>walker</role> </actor> </actors>You can see I added a new root element of Actors. If you dont, you will get an error that I was talking you about!
Ok, next, you can use dot notation linq version, similar to Sambath`s solution:
string path = @"C:\1\testXml03.xml"; XDocument xDoc = XDocument.Load(path); var items = xDoc.Descendants("actor").Where(w => w.Element("role").Value == "driver"); foreach (var item in items) { Console.WriteLine(string.Format("Id: {0}, Name: {1}", item.Element("id").Value, item.Element("name").Value)); } Console.ReadLine();
Hope it helps,
bye
Mitja
- Proposto come risposta Jason Dot WangMicrosoft Contingent Staff, Moderator mercoledì 8 agosto 2012 06:47
- Contrassegnato come risposta Jason Dot WangMicrosoft Contingent Staff, Moderator martedì 14 agosto 2012 06:43
-
lunedì 6 agosto 2012 16:55
Alternatively:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { XmlDocument xml = new XmlDocument(); xml.Load("example.xml"); XmlNode node = xml.SelectSingleNode("//actor[role='driver']"); //Write results Console.WriteLine("Name: {0}", node["name"].InnerText); Console.WriteLine("ID: {0}", node["id"].InnerText); Console.ReadLine(); } } }
- Proposto come risposta Jason Dot WangMicrosoft Contingent Staff, Moderator mercoledì 8 agosto 2012 06:47
- Contrassegnato come risposta Jason Dot WangMicrosoft Contingent Staff, Moderator martedì 14 agosto 2012 06:43
-
lunedì 6 agosto 2012 18:08That works! Thanks!
-
lunedì 6 agosto 2012 18:11Mitja - Thanks for your response! I understand the XML isn't "proper" as it is just a sample of an overall larger XML file. I guess I didn't feel the need to provide the whole thing to convey the point of my question. Next time I will make sure to do so...
-
lunedì 6 agosto 2012 19:08
:)
ok, do it.
Mitja

