I have a file, the contents in the file look like XML. So, I don't know I can parse the content in files and binding data to
DataTable
.
<School>
<StudentOneSubject>
<Member personId="1" active="yes">
<Name id="Mary"/>
<Subject>Maths</Subject>
</Member>
<Member personId="2" active="yes">
<Name id="John"/>
<Subject>Literature</Subject>
</Member>
</StudentOneSubject>
<StudentListSubject>
<Member personId="1" active="yes">
<Name id="Mary"/>
<SubjectList>
<Subjectvalue>Maths</Subjectvalue>
<Subjectvalue>Literature</Subjectvalue>
</SubjectList>
</Member>
<Member personId="2" active="yes">
<Name id="John"/>
<SubjectList>
<Subjectvalue>Physics</Subjectvalue>
<Subjectvalue>Maths</Subjectvalue>
<Subjectvalue>Literature</Subjectvalue>
</SubjectList>
</Member>
</StudentListSubject>
</School>
Probably, I need two
Datatable
s and the content I want like this
DataTable1
will have this content of
StudentOneSubject
, 3 columns:
ID#Name#Subject
1#Mary#Maths
2#John#Literature
And
DataTable2
will have these contents of
StudentListSubject
, 3 columns:
ID#Name#Subject
1#Mary#Maths,Literature
2#John#Physics,Maths,Literature
So, if there is any way to be flexible on this structure, please share ideas with me.
What I have tried:
Finding some advice, good idea for this case.
var _dt = new DataTable();
_dt.Columns.Add("ID", typeof(string));
_dt.Columns.Add("Name", typeof(string));
_dt.Columns.Add("Subject", typeof(string));
var _rd = XmlReader.Create("C:\\test.xml");
_rd.ReadToFollowing("Member");
do
{
var _str = "";
_rd.MoveToFirstAttribute();
_str += _rd.Value;
_rd.ReadToFollowing("Name");
_rd.MoveToFirstAttribute();
_str += "#" + _rd.Value;
_rd.ReadToFollowing("SubjectList");
_rd.ReadToFollowing("Subjectvalue");
_str += "#" + _rd.ReadElementContentAsString();
_dt.Rows.Add(_str.Split('#'));
} while (_rd.ReadToFollowing("Member"));