Home > Mobile >  Start reading from last value in xml
Start reading from last value in xml

Time:01-30

I have this kind of xml file. Root node is Sim and there are multiple "test" subnodes. Also each "test" has an instructor. Finally each instructor has own name,surname and rank values.

<Sim>
  <Test ID="1" Description="test1" Date="17/01/2023">
    <Instructor Num="1">
      <Name>instructor1</Name>
      <Surname></Surname>
      <Rank></Rank>
    </Instructor>
  </Test>
  <Test ID="2" Description="test2" Date="16/01/2023">
    <Instructor Num="22">
      <Name>instructor22</Name>
      <Surname></Surname>
      <Rank></Rank>
    </Instructor>
  </Test>
</Sim>

With xelement.load, the test ids of this file are read as they are in the file. I want xelement.load to read after test ids are sorted from largest to smallest. How can i do this? instructor part is also included in second code.

<Sim>
  <Test ID="3" Description="test3" Date="18/01/2023"></Test>
  <Test ID="2" Description="test2" Date="17/01/2023"></Test>
  <Test ID="1" Description="test1" Date="16/01/2023"></Test>
</Sim>

CodePudding user response:

Try with XDocument and LINQ

var xDoc = XDocument.Parse(File.ReadAllText("XMLFile2.xml"));
var elements = xDoc.Descendants("Test").OrderByDescending(x=> Convert.ToInt16(x.Attribute("ID").Value));
  • Related