您需要将循环属性通过子节点移出循环:
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 IsDefaultNamespaceDeclaration(this XmlAttribute attr) { if (attr == null) return false; if (attr.NamespaceURI != "http://www.w3.org/2000/xmlns/") return false; return attr.Name == "xmlns"; } 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实例。您还可以将所有类型的所有节点的文本显式设置
XmlElement为名称+属性数据,而不仅仅是具有子元素的元素,
OuterXml仅用于文本节点:
private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode) { if (inXmlNode is XmlElement) { // An element. Display element name + attribute names & values. foreach (var att in inXmlNode.Attributes.Cast<XmlAttribute>().Where(a => !a.IsNamespaceDeclaration())) { inTreeNode.Text = inTreeNode.Text + " " + att.Name + ": " + att.Value; } // Add children foreach (XmlNode xNode in inXmlNode.ChildNodes) { var tNode = inTreeNode.Nodes[inTreeNode.Nodes.Add(new TreeNode(xNode.Name))]; AddNode(xNode, tNode); } } else { // Not an element. Character data, comment, etc. Display all text. inTreeNode.Text = (inXmlNode.OuterXml).Trim(); } treeView1.ExpandAll(); }
如果您确实只想过滤掉默认的名称空间定义,而保留其他名称空间定义,则可以执行以下 *** 作:
// An element. Display element name + attribute names & values. foreach (var att in inXmlNode.Attributes.Cast<XmlAttribute>().Where(a => !a.IsDefaultNamespaceDeclaration())) { inTreeNode.Text = inTreeNode.Text + " " + att.Name + ": " + att.Value; }
顺便说一句,我不建议这样做,因为以下内容实际上是同一件事,即
DataConfiguration在名称空间中具有本地名称的元素
http://somenamespace:
<ss:DataConfiguration xmlns:ss="http://somenamespace"/><DataConfiguration xmlns="http://somenamespace"/>
您的树将显示第一个元素的名称空间,而不显示第二个元素的名称空间。
更新2
要将包含
XmlDeclaration在树中,请将顶级循环更改为:
treeView1.Nodes.Clear(); foreach (XmlNode xNode in dom.ChildNodes) { var tNode = treeView1.Nodes[treeView1.Nodes.Add(new TreeNode(xNode.Name))]; AddNode(xNode, tNode); }
更新3
将循环包括在以下内容
XmlDeclaration中
DisplayTreeView:
private void DisplayTreeView(string pathname) { try { // SECTION 1. Create a DOM document and load the XML data into it. Xmldocument dom = new Xmldocument(); dom.Load(pathname); // SECTION 2. Initialize the TreeView control. treeView1.Nodes.Clear(); // SECTION 3. Populate the TreeView with the XML nodes. foreach (XmlNode xNode in dom.ChildNodes) { var tNode = treeView1.Nodes[treeView1.Nodes.Add(new TreeNode(xNode.Name))]; AddNode(xNode, tNode); } } catch (XmlException xmlEx) { MessageBox.Show(xmlEx.Message); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)