Gautam Buddha Temple Varanasi,Varanasi - Buddhist Pilgrimage

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:  
 <Set>  
  <Setting>  
   <Name>username1</Name>  
   <ID>1234</ID>    
   <Add>Mumbai</Add>  
   <Path>name/userdata/img</Path>  
  </Setting>  
  <Setting>  
   <Name>username2</Name>  
   <ID>4534</ID>    
   <Add>Navi Mumbai</Add>  
   <Path>name/userdata/img</Path>  
  </Setting>  
 </Settings>  
 Solution:  
 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"))  
            .SingleOrDefault();  

1 comment:

  1. This blog having the details of Processes running. The way of runing is explained clearly. The content quality is really great. The full document is entirely amazing. Thank you very much for this blog.
    Seo Company in Chennai

    ReplyDelete