Domanda

Why doesn't this xpath work with me? I want the current title if the language _id =2

./title[language_id=2]

<news pnumber="1" id="1"><event_id>578</event_id><event_type_id>1</event_type_id><language_id>2</language_id><title>meeting</title></news>



<news pnumber="1" id="1"><event_id>578</event_id><event_type_id>1</event_type_id><language_id>1</language_id><title>meeting</title></news>
È stato utile?

Soluzione

The correct XPath expression is

//title[../language_id=2]

Altri suggerimenti

To avoid the reverse axis, use:

self::*[language_id=2]/title.

First add root to your XML then:

XDocument doc = XDocument.Load(xmlFilePath);

var result=  doc.Descendants("news")
             .Where(x=>x.Attribute("id") != null && x.Attribute("id").Value = "1")
             .Select(x=>x.Descendants("title").First().Value);

Here is description of this linq2xml:

First Load XML file (Also you can parse xml string):

XDocument.Load(xmlFilePath);

Find your news elements :

doc.Descendants("news")

Between news elements select elements which has id=1:

Where(x=>x.Attribute("id") != null && x.Attribute("id").Value = "1")

From each filtered item select first title:

x.Descendants("title").First().Value
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top