Populate treeview based on xml, but check attribute if it should display

JERRY Warra 121 Reputation points
2021-06-11T14:09:08.913+00:00

I have an internal website that has 2 similar pages that will display some of the same documents and some that are page specific.

Is it possible to populate a treeview based on the XML file where 2 attributes are that indicate Y/N if that node should be displayed on that page?

 <docs>
    <doc Text="Data1" p1="Y" p2="N">
        <file Text="Name" p1="Y" p2="N">
            <type p1="Y" p2="N" text="PDF" url="www.data1.tmp" />         
        </file>
    </doc>
    <doc Text="Data2" p1="N" p2="Y">
        <file Text="Name" p1="N" p2="Y">
            <type p1="N" p2="Y" text="docx" url="www.data2.tmp" />            
        </file>
    </doc>
    <doc Text="Data3" p1="Y" p2="Y">
        <file Text="Name" p1="Y" p2="Y">
            <type p1="Y" p2="Y" text="txt" url="www.data3.tmp" />         
        </file>
    </doc>    
</docs>

PAGE 1

Data1
..Name
....PDF *Link using URL attribute
Data3
..Name
....txt *Link using URL attribute

PAGE 2

Data2
..Name
....docx *Link using URL attribute
Data3
..Name
....txt *Link using URL attribute

I want to be able to update the XML file and have the trees rendered based on the XML file. I don't want to have to include and exclude in the code behind every time a new document is added.

Thank you for any help

Developer technologies | C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,656 Reputation points
    2021-06-14T01:24:34.367+00:00

    Hi JERRYWarra-9978,
    You can try to use linq xml to populate treeview from XML.
    Code looks like:

    var data = XElement.Load(source.xml);  
    foreach (XElement single in data.Elements())  
    {  
      treeNode.Nodes.Add(single.Element("name of element").Value);  
    }  
    

    Moe details you can refer to this thread.
    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.