保存所有节点属性XML

时间:2015-11-24 12:35:29

标签: c# xml treenode

我编写了一个代码,用于读取 public TourOffice findOfficeNameByLogin(String username) { Criteria name = createCriteria(); name.createAlias("login", "login"); name.add(Restrictions.eq("login.username", username)); return (TourOffice) name.uniqueResult(); } 文件,然后使用节点名称构建XMl文件的树视图。我想知道我怎么能有属性而不是组件(节点)名称?

例如,在以下XML文件而不是XML(s)中,我想在树视图actioncopy等中打印,除了前两个父节点(pasteReport)。

test档案:

XML

<Report version="2.1" xmlns="http://www.froglogic.com/XML2"> <test name="Example"> <action name="delet"> this is delet </action> <action name="copy"> this is copy </action> <action name="paste"> this is paste </action> <action name="manipulate"> this is manipulate </action> <action name="copy"> this is copy </action> <action name="paste"> this is paste </action> <action name="manipulate"> this is manipulate </action> <action name="change"> this is change </action> </test> </Report> 代码:

C#

更新

private void File2_load(object sender, EventArgs e)
    {
        try
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(File2Path.Text);

            treeView2.Nodes.Clear();
            treeView2.Nodes.Add(new TreeNode(doc.DocumentElement.Name));
            TreeNode tNode = new TreeNode();
            tNode = treeView2.Nodes[0];

            AddNode(doc.DocumentElement, tNode);
            treeView2.ExpandAll();
            treeView2.CheckBoxes = true;
        }
        catch (XmlException xmlEx)
        {
            MessageBox.Show(xmlEx.Message);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

1 个答案:

答案 0 :(得分:0)

更改

            xNode = inXmlNode.ChildNodes[i];
            inTreeNode.Nodes.Add(new TreeNode(xNode.Name));

            xNode = inXmlNode.ChildNodes[i];
            TreeNode childTreeNode;
            if (xNode.LocalName == "action" && xNode.NodeType == XmlNodeType.Element)
            {
              childTreeNode = new TreeNode(xNode.Attributes["name"].Value);
            }
            else
            {
              childTreeNode = new TreeNode(xNode.Name);
            }

            inTreeNode.Nodes.Add(childTreeNode);

您可能希望在NodeTypeLocalName上添加进一步的检查,具体取决于XML输入的复杂性以及XML输入中可能包含的可能节点。