Search code examples
c#xmlwildcardxelement

Wildcard Search in XElement


I am using XElement to search a XML document that a user selects through openfiledialog.

Here is the code:

private void dothis()
    {
        string query;

        XElement xml = XElement.Load(path);
        XNamespace ns = xml.GetDefaultNamespace();
        IEnumerable<XElement> symptoms = 


        from item in xml.Descendants(ns + "section")

        where (string) item.Element(ns + "text") == queryString.Text
        select item;

        // Execute the query 
        foreach (var complaints in symptoms)
        {
            // Instantiate the writer
            _writer = new TextBoxStreamWriter(txtConsole);
            // Redirect the out Console stream
            Console.SetOut(_writer);


            Console.WriteLine(complaints);

        }

        //Pause the application 
        Console.ReadLine();
    }

I want to make the query that is based on queryString.text be a wildcard.

So the text field might contain Confusion, Nausea, Headache

If I just type confusion into my queryString text box, then I want it still to located that element and node.

Thank you


Solution

  • So it sounds like all you want is:

    where item.Element(ns + text).Value.Contains(queryString.Text)
    

    Would that work for you?