Berat Akgül
Berat Akgül

Reputation: 152

In linq getting synonym of word

I want to get the synonym of word in c#. For instance, merhaba - hello or selam-hi. I can only do "merhaba" to "hello" but i can't access the other nodes.(merhaba-hi or selam-hi) How can i do this ? Thanks.

My XML file.

<Words>
    <Meaning>
        <Turkish type="noun">merhaba</Turkish>
        <Turkish type="noun">selam</Turkish>
        <English type="noun">hello</English>
        <English type="noun">hi</English>
    </Meaning>
</Words>

My query was like that.

var word = from p in doc.Elements("Words").Elements("Meaning")
             where textBox1.Text == p.Element("Turkish").Value
             select new
             {
                _word = p.Element("Turkish").Value,
                meaning = p.Element("English").Value,
                kind = p.Element("English").Attribute("type").Value
             };

Upvotes: 0

Views: 395

Answers (1)

Jules
Jules

Reputation: 559

You'll probably want to try something like this:

var word = from p in doc.Elements("Words").Elements("Meaning")
           where p.Elements("Turkish").Any(item => item.Value == textBox1.Text)
           from synonym in p.Elements("English")
           select new
           {
               _word = textBox1.Text,
               meaning = synonym.Value,
               kind = synonym.Attribute("type").Value
           };

The expression p.Elements("Turkish").Any(item => item.Value == textBox1.Text) looks for a meaning element that contains the desired word. The from synonym in p.Elements("English") line runs through all elements with the name English.

Upvotes: 1

Related Questions