Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
XML
<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 Datatables 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.

C#
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; //get personID
    _rd.ReadToFollowing("Name");
    _rd.MoveToFirstAttribute();
    _str += "#" + _rd.Value; //get ID

    ////get subject list, but only get first subject element
    _rd.ReadToFollowing("SubjectList");
    _rd.ReadToFollowing("Subjectvalue");
    _str += "#" + _rd.ReadElementContentAsString();

    _dt.Rows.Add(_str.Split('#'));
} while (_rd.ReadToFollowing("Member"));
Posted
Updated 14-Sep-23 2:32am
v5

1 solution

 
Share this answer
 
Comments
headshot9x 23-Aug-23 6:38am    
Hi, but can we separate extract data to 2 DataTable from one .xml file?
Richard MacCutchan 23-Aug-23 6:42am    
I am not sure, check the documentation. But if it cannot do that then you will most likely need to read it manually using the simple XmlReader, or LINQToXML. Again you need to consult the documentation to see which is the optimum choice for you.
headshot9x 24-Aug-23 3:02am    
I tried it, I can get personID and ID, but how can I get Subject list?
Richard MacCutchan 24-Aug-23 3:43am    
Did you try the suggestion I gave you above?
headshot9x 24-Aug-23 3:56am    
yes, I tried with some code above.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900