Home > DeveloperSection > Forums > How to check a string exists in a xml node's value asp.net
Chintoo Semi

Total Post:135

Points:947
Posted on    May-02-2013 3:07 AM

 XML XML 
Ratings:


 1 Reply(s)
 1502  View(s)
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! 



AVADHESH PATEL

Total Post:604

Points:4228
Posted on    May-02-2013 8:12 AM

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

Don't want to miss updates? Please click the below button!

Follow MindStick