How to check a string exists in a xml node's value

Total Post:135


 2808  View(s)
Rate this:
Hi Everyone!

My xml will be like below

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

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

Please help me!

Any secession will be welcome!


  1. Re: How to check a string exists in a xml node's value

    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);


Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.