HOW TO CHECK A STRING EXISTS IN A XML NODE'S VALUE ASP.NET

Chintoo Semi

Total Post:135

Points:947
Posted by  Chintoo Semi
XML 
XML
 1922  View(s)
Ratings:
Rate this:
Hi Everyone!

My xml will be like below

<Employee>
      <Emp>
        <Name id="1" link="/office1/manager"></Name>
        <Name id="2" link="/office/sweeper"></Name>
        <Name id="3" link="/office2/manager"></Name>
       </Emp>
  </Employee>

I want to get the "id" of the employees who contains string "manager" in "link"

Please help me!

Any secession will be welcome!

Thanks! 

  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: How to check a string exists in a xml node's value asp.net

    Hi Chinto!

    Using linq to xml:

    XDocument doc = XDocument.Load("XMLFilePath");
    var selectors = from elements in doc.Elements("Employee").Elements("Emp").Elements("Name")
                    where elements.Attribute("link").Value.Contains("manager")
                    select elements;

    string ids = string.Empty;
    foreach (var element in selectors)
    {
            ids  += element.Attribute("id").Value + ",";
    }
    Also, for loading from string you can use:

    XDocument doc = XDocument.Parse(xmlString);

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!