Monday 7 April 2014

Access the multiple attributes of XML File with LINQ in C#

Xml File 

StudentInfo.xml










Xml file contains attribute named as Name, USN, Address

LINQ QUERY to fetch particular Student Information:

private List<string> GetStudentDetails(string _usn)
{
  List<string> _lstStudentInfo=new List<string>();
  XmlDocument xdcDocument = new XmlDocument();
  XElement root =XDocument.Load(@"H:\CollegeSeminar\StudentInfo.xml").Root;
  
  var result = root.Elements("Student").
         Where(x => x.Attribute("USN").Value == _studentId).                                
         Select(i => new { Name = i.Attribute("Name").Value,
         StudentAddress = i.Attribute("Address").Value,StudentUSN= i.Attribute("USN").Value }).ToList();
  //To get the values
 _lstStudentInfo.Add(result[0].Name);
_lstStudentInfo.Add(result[0].StudentAddress);
_lstStudentInfo.Add(result[0].StudentUSN);
 return _lstStudentInfo;
 
 }

Get CSV Values in SQL

Query: DECLARE @CSVValue NVARCHAR(50)='100,101,102'                   DECLARE @eachValue NUMERIC(9,0)   while len(@CSVValue) &...