Rumah >pembangunan bahagian belakang >C++ >Bagaimana untuk Mencegah Paparan Atribut Pendua Apabila Menghuraikan XML dengan Atribut ke dalam C# TreeView?
dan gunakannya untuk melangkau contoh xmlattribute yang tidak diingini:
<?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">Teks atribut dan elemen pada semua nod:
Untuk memastikan bahawa semua nod memaparkan nama elemen dan data atribut mereka, bukan hanya mereka yang mempunyai anak, gunakan OUTERXML hanya untuk nod teks:
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(); }
Atas ialah kandungan terperinci Bagaimana untuk Mencegah Paparan Atribut Pendua Apabila Menghuraikan XML dengan Atribut ke dalam C# TreeView?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!