locked
Seek a string value term in an XML document RRS feed

  • Question

  • User269881539 posted

    I have an XML file that represents a thesaurus of sorts (basic example) :

    <?xml version="1.0" encoding="utf-8" ?>
    <synonyms>
      <group>
        <syn>spanner</syn>
        <syn>wrench</syn>
      </group>
      <group>
        <syn>lawnmower</syn>
        <syn>lawn mower</syn>
        <syn>grass cutter</syn>
      </group>
    </synonyms>

    I need to match this to an incoming string and expand that string by all of the terms in the matching group - for example :

    If someone enters the term "rotary lawnmower" I need to first match the group that contains the term "lawnmower" and then grab all the terms within that group - then my expanded string will become :

    "rotary lawnmower lawn mower grass cutter"

    I need an efficient way to do this without having to loop each word of my original string AND then the whole XML file for each word. In my head I am thinking I need to seek the term, and then grab the rest of the terms from within the group - but I can't fathom quite how to do this (and especially efficiently!)

    Thanks.

    Thursday, November 30, 2017 4:02 PM

Answers

  • User-707554951 posted

    Hi chilluk,

    Working sample as below:

    <asp:TextBox ID="TextBox1" runat="server" 
                OnTextChanged="TextBox1_TextChanged" AutoPostBack="true"></asp:TextBox>

    Code behind:

    using System.Xml.Linq;
      protected void TextBox1_TextChanged(object sender, EventArgs e)
            {
                string search = TextBox1.Text;
                List<string> list1 = new List<string>();
                list1.Add(search);
                XDocument doc = XDocument.Load(Server.MapPath("Success.xml"));
                var s = (doc.Descendants("synonyms").Descendants("group")
                         .ToList()
                         .Where(c => c.Descendants("syn")
                                .Select(d => new { value = d.Value }).ToList()
                                .Any(f => list1.Any(l => l.ToString().Contains(f.value)))
                                )).Elements("syn").Select(ds=>ds.Value).ToList();
                var result = String.Join(" ", s.ToArray());
              Response.Write(result.Replace("lawnmower", "rotary lawnmower"));
            }

    Output:

    Best regards 

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 1, 2017 7:43 AM

All replies

  • User-707554951 posted

    Hi chilluk,

    Working sample as below:

    <asp:TextBox ID="TextBox1" runat="server" 
                OnTextChanged="TextBox1_TextChanged" AutoPostBack="true"></asp:TextBox>

    Code behind:

    using System.Xml.Linq;
      protected void TextBox1_TextChanged(object sender, EventArgs e)
            {
                string search = TextBox1.Text;
                List<string> list1 = new List<string>();
                list1.Add(search);
                XDocument doc = XDocument.Load(Server.MapPath("Success.xml"));
                var s = (doc.Descendants("synonyms").Descendants("group")
                         .ToList()
                         .Where(c => c.Descendants("syn")
                                .Select(d => new { value = d.Value }).ToList()
                                .Any(f => list1.Any(l => l.ToString().Contains(f.value)))
                                )).Elements("syn").Select(ds=>ds.Value).ToList();
                var result = String.Join(" ", s.ToArray());
              Response.Write(result.Replace("lawnmower", "rotary lawnmower"));
            }

    Output:

    Best regards 

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 1, 2017 7:43 AM
  • User269881539 posted

    That is quite awesome - thank you!

    Friday, December 1, 2017 11:06 AM
  • User269881539 posted

    Ah I wonder if there is a way to extend this?

    If the incoming string is multiple words, I wonder can it be matched back to the xml - for example with our lawnmower example in our file we have :

    lawnmower
    lawn mower
    grass cutter

    At the moment if someone enters "lawnmower" it matches and expands just great - but if they enter "lawn mower" then it doesn't because it's matching on individual words to what is coming in. I guess I maybe need to do it in reverse and compare the synonymns on the XML file back to the incoming string and map for individual or phrase matches?

    Friday, December 1, 2017 4:19 PM