Monday, August 11, 2014

Read a single node from xml (C# String) in C#/.net

 I want to pass the ID as a input parameter of the given xml that will search the XML document for that value within the <id> node.   
 Once ID found ,I want to find the next node and return it's name as a string.  
 Given xml as folloing:  
   <Add>Navi Mumbai</Add>  
 This Linq to xml query will return IEnumerbale<string> of value elements, which matched your id. You need to sure ID should be  
 unquine in given xml.  
 string value = doc.Descendants("Setting")  
            .Where(v => (string)v.Element("ID") == Id)  
            .Select(v => (string)v.Element("Name"))