none
Sorting child elements of xmlelements based on attribute value RRS feed

  • Question

  • Hello folks,

    I have the following type of xmlelement:

    <goal>
    <para>Maximize function </para>
    <status type="invisible">
    <para>vision</para>
    </status>
    <status type="offline">
    <para>training</para>
    </status>
    <status type="busy">
    <para>treatment</para>
    </status>
    <status type="busy">
    <para>lenses</para>
    </status>
    <goal>
    <goal>
    <para>Maximize function </para>
    <status type="visible">
    <para>vision</para>
    </status>
    <status type="busy">
    <para>training</para>
    </status>
    <status type="invisible">
    <para>treatment</para>
    </status>
    <status type="invisible">
    <para>Referral to optometrist for corrective lenses</para>
    </status>
    <goal>

    I want to sort status elements based on their type. I will get the sorting order as a list: 

    E.G. {visible,offline,busy,invisible} if this is the list I get, the elements will be sorted based on this order. I was be able to accomplish this using a bunch of nested loops but I want a better solution.

    Thanks


    Fisseha G

    Tuesday, October 29, 2013 9:35 PM

Answers

  • Hello,

    For this, my suggestion is to use the LINQ to XML. We can use codes as below:

    internal void Execute()
    
            {
    
                string xmlFile = "E:\\BMX\\Lab\\SmapleFile\\2013-10\\Sample_31.xml";
    
    
                XDocument xDoc = XDocument.Load(xmlFile);
    
    
                var storeResult = xDoc.Element("goal")
    
                     .Elements("para").Union(xDoc.Element("goal")
    
                     .Elements("status")
    
                     .OrderBy(s => (int)s.Attribute("order")));
    
    
                XDocument doc = new XDocument(new XElement("goal", storeResult));
    
                doc.Save("E:\\BMX\\Lab\\SmapleFile\\2013-10\\storeResult.xml");
    
    
            }
    

    The result:

    <?xml version="1.0" encoding="utf-8"?>
    
    <goal>
    
      <para>Maximize function </para>
    
      <status type="visible" order="1">
    
        <para>lenses</para>
    
      </status>
    
      <status type="offline" order="2">
    
        <para>training</para>
    
      </status>
    
      <status type="busy" order="3">
    
        <para>treatment</para>
    
      </status>
    
      <status type="busy" order="3">
    
        <para>lenses</para>
    
      </status>
    
      <status type="invisible" order="4">
    
        <para>vision</para>
    
      </status>
    
    </goal>
    

    And we need to add an additional attribute for element status, because we need use this attribute for the order filed.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, October 31, 2013 6:25 AM
    Moderator