Question:
When using the Foreach cycle to parse the XML file containing attributes, the attributes will be displayed multiple times. How to modify the code to display only one attribute?
Solution:
Only display only one attribute in the TreeView control:
Explanation:
private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode) { if (inXmlNode.HasChildNodes) { // 检查 XmlNode 是否具有属性 string attributes = ""; foreach (XmlAttribute att in inXmlNode.Attributes) { attributes += " " + att.Name + ": " + att.Value; } if (!string.IsNullOrEmpty(attributes)) { inTreeNode.Text += attributes; } foreach (XmlNode node in inXmlNode.ChildNodes) { TreeNode newNode = inTreeNode.Nodes.Add(node.Name); AddNode(node, newNode); } } }
to accumulate all the attributes, and then add it to at one time to avoid repeatedly added.
The above is the detailed content of How to Avoid Duplicate Attribute Display When Parsing XML into a C# TreeView?. For more information, please follow other related articles on the PHP Chinese website!