將帶有屬性的XML 文件解析為C# 結果中的樹視圖在為其擁有的每個子節點多次顯示的屬性中。如何修改代碼才能保證屬性只顯示一次?
<?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">
只顯示節點的屬性一次,將通過屬性的循環移出通過子節點的循環。
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(); }
過濾命名空間屬性:
如果要刪除命名空間屬性的顯示,請使用以下擴展方法:
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:"); } }
並使用它來跳過不需要的XmlAttribute 實例:
foreach (var att in inXmlNode.Attributes.Cast<XmlAttribute>().Where(a => !a.IsNamespaceDeclaration())) { inTreeNode.Text = inTreeNode.Text + " " + att.Name + ": " + att.Value; }
顯示所有節點上的屬性和元素文本:
要確保所有節點顯示其元素名稱和屬性數據,而不僅僅是具有子節點的節點,請僅對文本節點使用OuterXml:
private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode) { if (inXmlNode is XmlElement) {
以上是當用屬性解析XML到C#TreeView時,如何防止重複屬性顯示?的詳細內容。更多資訊請關注PHP中文網其他相關文章!