How to check a particular Element has a value or inner elements? here I have a list of Students, by using the name as search key displaying their results to richtextbox but here I have a problem where it comes to smith like students, I have one more table inside it as a list of marks, how to check and display Do that particular element have value or elements? I am using C# and LINQ to retrieve the data.
<StudentDataset>
<Student>
<Name>Raj</Name>
<TotalMarks>330</TotalMarks>
<Year>2009+5</Year>
</Student>
<Student>
<Name>Dennis</Name>
<TotalMarks>514</TotalMarks>
<Year>2009</Year>
</Student>
<Student>
<Name>Lisa</Name>
<TotalMarks>510</TotalMarks>
<Year>2011</Year>
</Student>
<Student>
<Name>Rahul</Name>
<TotalMarks>490</TotalMarks>
<Year>2019</Year>
</Student>
<Student>
<Name>Smith</Name>
<TotalMarks>
<IndividualScores>
<Table Mode="SSC">
<Item Marks="40" Result="10.6" />
<Item Marks="100" Result="6.4" />
<Item Marks="110" Result="5.7" />
<Item Marks="120" Result="5" />
<Item Marks="130" Result="4.3" />
<Item Marks="140" Result="3.5" />
<Item Marks="150" Result="2.8" />
</Table>
<Table Mode="Inter">
<Item Marks="40" Result="8.8" />
<Item Marks="50" Result="8.8" />
<Item Marks="60" Result="8.8" />
<Item Marks="70" Result="8.8" />
<Item Marks="80" Result="8.8" />
</Table>
</IndividualScores>
</TotalMarks>
<Year>2013</Year>
</Student>
<Student >
<Name>Jessy </Name>
<TotalMarks><SchoolName xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> /.>
<English>88</English>
<Mathematics>90</mathematics>
<Science>98</science>
</SchoolName>
</TotalMarks>
<Year>2014</Year>
</Student>
</StudentDataset>
C#
XDocument doc = XDocument.Load(@"filename");
IEnumerable<XElement> AllStudntElements = doc.Root.Elements();
var SelectDataByName = from element in AllStudntElements
where element.Element("Name").Value == SearchtItem
select element;
using for each loop it is working perfectly for every student but like smith how to check whether total marks have inner elements or not and how to display Marks and Results as a list for smith like students? I have tried everything like has children,hasAttribute but nothing giving proper results?
For Jesse, I have XML schema, for Smith, I have a table How to know whether an Element has value or element or schema?
Very New to XML, what to use XmlReader and how to Crack this thing? been trying from 4 days constantly on this but no idea...
Try following using xml linq :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Data;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
List<Student> students = Student.GetStudents(FILENAME);
DataTable dt = Student.CreateTable(students);
}
}
public class Student
{
public string Name { get; set; }
public string Year { get; set; }
public int? intTotalMarks { get; set; }
public List<TotalMarks> totalMarks { get; set; }
public static List<Student> GetStudents(string filename)
{
XDocument doc = XDocument.Load(filename);
List<Student> students = new List<Student>();
foreach (XElement xStudent in doc.Descendants("Student"))
{
Student student = new Student();
students.Add(student);
student.Name = (string)xStudent.Element("Name");
student.Year = (string)xStudent.Element("Year");
XElement xMarks = xStudent.Element("TotalMarks");
if (xMarks.HasElements)
{
foreach (XElement table in xStudent.Descendants("Table"))
{
TotalMarks totalMarks = new TotalMarks();
if (student.totalMarks == null) student.totalMarks = new List<TotalMarks>();
student.totalMarks.Add(totalMarks);
totalMarks.mode = (string)table.Attribute("Mode");
totalMarks.marks = table.Elements("Item").Select(x => new Mark()
{
mark = (int)x.Attribute("Marks"),
result = (decimal)x.Attribute("Result")
}).ToList();
}
}
else
{
student.intTotalMarks = (int)xMarks;
}
}
return students;
}
public static DataTable CreateTable(List<Student> students)
{
DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Year", typeof(string));
dt.Columns.Add("Mode", typeof(string));
dt.Columns.Add("Mark", typeof(int));
dt.Columns.Add("Result", typeof(decimal));
foreach(Student student in students)
{
if (student.totalMarks == null)
{
dt.Rows.Add(new object[] {
student.Name,
student.Year,
null,
student.intTotalMarks
});
}
else
{
foreach (TotalMarks totalMark in student.totalMarks)
{
foreach (Mark mark in totalMark.marks)
{
dt.Rows.Add(new object[] {
student.Name,
student.Year,
totalMark.mode,
mark.mark,
mark.result
});
}
}
}
}
return dt;
}
}
public class TotalMarks
{
public string mode { get; set; }
public List<Mark> marks { get; set; }
}
public class Mark
{
public int mark { get; set; }
public decimal result { get; set; }
}
}