Home >Backend Development >C++ >How to Prevent Duplicate Attribute Display When Parsing XML with Attributes into a C# TreeView?

How to Prevent Duplicate Attribute Display When Parsing XML with Attributes into a C# TreeView?

Susan Sarandon
Susan SarandonOriginal
2025-01-27 11:46:12295browse

How to Prevent Duplicate Attribute Display When Parsing XML with Attributes into a C# TreeView?

Recursion: Parsing XML File with Attributes into TreeView in C

Problem:

Parsing an XML file with attributes into a tree view in C# results in the attributes being displayed multiple times for each child node it has. How can the code be modified to ensure that attributes are only displayed once?

XML File Example:

<?xml version="1.0" encoding="utf-8"?>
<DataConfiguration xmlns="abcefg12345" xmlns:xsi="12345abcefg" xsi:schemaLocation="12345abcefg12345abcefg">
  <Hosts>
    <Sites>
        <Site Name="ss">
            <Host Id="aa">
                <Address Host="www.www.com"></Address>
            </Host>
            <Host Id="ee">
                <Address Host="www.www.com"></Address>
            </Host>
            <Host Id="dd">
                <Address Host="www.www.com"></Address>
            </Host> 
            <Host Id="pp">
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com" />
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com/" />
            </Host>
            <Host Id="ss">
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com" />
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com" />
            </Host> 
            <Host Id="561">
                <Address Host="www.www.com"></Address>
            </Host> 
        </Site>
        <Site Name="hihi">
            <Host Id="cc">
                <Address Host="www.www.com"></Address>
            </Host>
            <Host Id="sdD">
                <Address Host="www.www.com"></Address>
            </Host>
            <Host Id="8uj">
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com" />
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com" /> 
            </Host>
            <Host Id="222">
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com" />
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com" />               
            </Host>
            <Host Id="hhh">
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com" />
            </Host>
            <Host Id="hhh">
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com" />
            </Host>             
        </Site>     
    </Sites>
            <Host Id="hhh">
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com" />
            </Host>
            <Host Id="hhh">
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com" />
            </Host>
            <Host Id="hhh">
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com" />
            </Host>         
            <Host Id="hhh">
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com" />
            </Host>

</Hosts>
<DataPools>
    <DataPool Id="sss" default="sure">
        <DataGroup Id="sss" Parent="aaa" UserCanSelectHost="sure">
            <HostId Parent="hhhh">I'm breaking here</HostId>
            <DataSources>
                <empty/>
            </DataSources>
        </DataGroup>
        <DataGroup Id="ccc" UserCanSelectHost="whynot">
            <HostId>God I'm breaking here again, i hope you can fix me</HostId>
            <DataSources>
                <empty/>
            </DataSources>
        </DataGroup>
        <DataGroup Id="sss" UserCanSelectHost="yessure">
            <HostId>cry face</HostId>
                <webfg displaygroup="sss" provider="sss">

Solution:

To display the attributes of a node only once, move the loop through the attributes out of the loop through the child nodes.

private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
{
    // Loop through the XML nodes until the leaf is reached.
    // Add the nodes to the TreeView during the looping process.
    if (inXmlNode.HasChildNodes)
    {
        //Check if the XmlNode has attributes
        foreach (XmlAttribute att in inXmlNode.Attributes)
        {
            inTreeNode.Text = inTreeNode.Text + " " + att.Name + ": " + att.Value;
        }

        var nodeList = inXmlNode.ChildNodes;
        for (int i = 0; i < nodeList.Count; i++)
        {
            var xNode = inXmlNode.ChildNodes[i];
            var tNode = inTreeNode.Nodes[inTreeNode.Nodes.Add(new TreeNode(xNode.Name))];
            AddNode(xNode, tNode);
        }
    }
    else
    {
        // Here you need to pull the data from the XmlNode based on the
        // type of node, whether attribute values are required, and so forth.
        inTreeNode.Text = (inXmlNode.OuterXml).Trim();
    }
    treeView1.ExpandAll();
}

Additional Enhancements:

Filter Out Namespace Attributes:

If you want to remove the display of namespace attributes, use the following extension method:

public static class XmlNodeExtensions
{
    public static bool IsNamespaceDeclaration(this XmlAttribute attr)
    {
        if (attr == null)
            return false;
        if (attr.NamespaceURI != "http://www.w3.org/2000/xmlns/")
            return false;
        return attr.Name == "xmlns" || attr.Name.StartsWith("xmlns:");
    }
}

And use it to skip unwanted XmlAttribute instances:

foreach (var att in inXmlNode.Attributes.Cast<XmlAttribute>().Where(a => !a.IsNamespaceDeclaration()))
{
    inTreeNode.Text = inTreeNode.Text + " " + att.Name + ": " + att.Value;
}

Display Attribute and Element Text on All Nodes:

To ensure that all nodes display their element name and attribute data, not just those with children, use OuterXml only for text nodes:

private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
{
    if (inXmlNode is XmlElement)
    {

The above is the detailed content of How to Prevent Duplicate Attribute Display When Parsing XML with Attributes into a C# TreeView?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn