2
Answers

Please tell me the = best Way to read XML, and store it in datatable(C#,Asp.net).

Vikas Ahlawat

Vikas Ahlawat

13y
3.1k
1
Hi i got the following XML
<Leg>
<From>LCY</From>
<To>AMS</To>
<DepDate>Sun 27Feb11</DepDate>
<DepTime>17:50</DepTime>
<MealCode></MealCode>
<Equip>AR8</Equip>
<Miles> 217</Miles>
<Time> 1:15</Time>
<ItemFRef>734185403402799</ItemFRef>
<Text> KL 2760 CITYJET</Text>
<LegType>1</LegType>
</Leg>

<Leg>
<From>AMS</From>
<To>CDG</To>
<DepDate>Sun 27Feb11</DepDate>
<DepTime>20:45</DepTime>
<MealCode>M</MealCode>
<Equip>321</Equip>
<Miles> 247</Miles>
<Time> 1:15</Time>
<ItemFRef>734185403402799</ItemFRef>
<Text> KL 2023 AIR FRANCE</Text>
<GroundTime> :40</GroundTime>

<LegType>1</LegType>
</Leg>

<Leg>
<From>CDG</From>
<To>AMS</To>
<DepDate>Thu 24Mar11</DepDate>
<DepTime>20:25</DepTime>
<MealCode>M</MealCode>
<Equip>737</Equip>
<Miles> 247</Miles>
<Time> 1:15</Time>
<ItemFRef>734185403402799</ItemFRef>
<LegType>2</LegType>
</Leg>

For store this in datatable i am using following strategies

//Code in sort form.....

xmldoc1.LoadXml(responseFromServer1); // I got the above XML here
XmlNodeList leg = xmldoc1.GetElementsByTagName("Leg");

for (int i = 0; i < leg.Count; i++)
{
for(...........
.................
dr1["From"] = leg[i].ChildNodes[0].InnerText.ToString(); // here i m storing the value of node in datatable
...............
...............
}

but I think it is not good code.
Can any one tell me the bast way to achieve my goal?

Answers (2)