0

嗨,我在 C# Windows 窗体中遇到了 xml 和树视图控件的问题。

我的应用程序:

  1. 首先,我从 cmd 命令创建一个字符串。这是 xml 格式的输出。final_output 是我的 XMLReader 字符串。

    字符串命令 = "\"C:\lmxendutil.exe\" -licstatxml -host lwserv005 -port 6200";

            string output = ExecuteCommand(command);
            string final_output = output.Substring(90, output.Length - 90);
    
  2. 其次,我使用 XMLReader 和 StringReader 读取这个字符串并在 xml 中解析它。比我用属性 NAME="..." 的名称为“FEATURE”的所有节点创建一个集合。

    XmlReader xr = XmlReader.Create(new StringReader(final_output));

        while (xr.Read())
        {
            switch (xr.Name)
            {
                case "FEATURE":
                    if (xr.HasAttributes)
                    {
                        while (xr.MoveToNextAttribute())
                        {
                            if (xr.Name == "NAME")
                            {
                                liste.Add(xr.Value);
    
                            }
    
                        }
    
                    }
                  break;
            }
        }
    
  3. 现在我在 TreeView 中显示我的列表。

    树节点节点;

            foreach (string x in liste)
            {
                node = new TreeNode(x);
                treeLic.Nodes.Add(node);
            }
    

XML 结构是这样的:

<FEATURE NAME="" VERSION="" .. >
  <USER NAME="" HOST="" ...>
  <USER NAME="" HOST="" ...>
  <USER NAME="" HOST="" ...>
<FEATURE NAME="" VERSION="" .. >
<FEATURE NAME="" VERSION="" .. >
<FEATURE NAME="" VERSION="" .. >
  <USER NAME="" HOST="" ...>
  <USER NAME="" HOST="" ...>
  <USER NAME="" HOST="" ...>

... 

我想用 FEATURE 和 USER 的属性 NAME 值为我的树视图获取这个结构。我的树视图只向我显示具有 NAME 属性值的 FEATURE。我用 readsubtree 尝试它,但它不起作用:(。

我的问题现在...

如何在树视图中显示具有属性 NAME 的所有节点,如果我单击树视图节点,我可以创建一个事件,但我如何使用它来显示有关 FEATURE 节点的子树节点的更多信息?

xml 示例:

<?xml version="1.0" encoding="UTF-8"?>
<LM-X STAT_VERSION="3.32">
<LICENSE_PATH TYPE="NETWORK" HOST="6200@serv005" SERVER_VERSION="4.4.4" UPTIME="53 day(s) 21 hour(s) 10 min(s) 50 sec(s)">
<FEATURE NAME="GlobalZoneEU" VERSION="12.0" VENDOR="ALTAIR" START="2013-03-26" END="2014-03-31" USED_LICENSES="111720" TOTAL_LICENSES="147000" SHARE="CUSTOM ,VIRTUAL">
<USER NAME="SYSTEM" HOST="SERV171" IP="172.16.11.115" USED_LICENSES="2000" LOGIN_TIME="2013-04-17 12:42" CHECKOUT_TIME="2013-04-17 12:42" SHARE_CUSTOM="hweuser:172.16.11.115"/>
>
<USER NAME="pbsadmin" HOST="LWSERV171" IP="172.16.11.115" USED_LICENSES="720" LOGIN_TIME="2013-04-17 12:44" CHECKOUT_TIME="2013-04-17 12:44" SHARE_CUSTOM="pbsadmin:LWSERV171:1592_40960072_1356792762_826820"/>
>
<USER NAME="Bell.M" HOST="DBG4327" IP="172.16.225.105" USED_LICENSES="21000" LOGIN_TIME="2013-05-21 07:52" CHECKOUT_TIME="2013-05-21 07:52" SHARE_CUSTOM="Bell.M:DBG4327"/>
>
<USER NAME="Bell.M" HOST="DBG4327" IP="172.16.225.105" USED_LICENSES="6000" LOGIN_TIME="2013-05-21 07:54" CHECKOUT_TIME="2013-05-21 07:54" SHARE_CUSTOM="Bell.M:DBG4327"/>
>
<USER NAME="xu.d" HOST="DBG4970" IP="172.16.225.126" USED_LICENSES="21000" LOGIN_TIME="2013-05-21 08:15" CHECKOUT_TIME="2013-05-21 08:15" SHARE_CUSTOM="xu.d:DBG4970"/>
>
<USER NAME="xu.d" HOST="DBG4970" IP="172.16.225.126" USED_LICENSES="21000" LOGIN_TIME="2013-05-21 08:18" CHECKOUT_TIME="2013-05-21 08:18" SHARE_CUSTOM="xu.d:DBG4970"/>
>
<USER NAME="Bell.M" HOST="DBG4327" IP="172.16.225.105" USED_LICENSES="21000" LOGIN_TIME="2013-05-21 08:23" CHECKOUT_TIME="2013-05-21 08:23" SHARE_CUSTOM="Bell.M:DBG4327"/>
>
<USER NAME="xu.d" HOST="SIM02" IP="172.16.32.12" USED_LICENSES="21000" LOGIN_TIME="2013-05-21 08:36" CHECKOUT_TIME="2013-05-21 08:36" SHARE_CUSTOM="xu.d:SIM02"/>
>
<USER NAME="xu.d" HOST="SIM02" IP="172.16.32.12" USED_LICENSES="6000" LOGIN_TIME="2013-05-21 08:37" CHECKOUT_TIME="2013-05-21 08:37" SHARE_CUSTOM="xu.d:SIM02"/>
>
<USER NAME="hassan.a" HOST="DBG5199" IP="172.16.225.114" USED_LICENSES="21000" LOGIN_TIME="2013-05-21 09:24" CHECKOUT_TIME="2013-05-21 09:24" SHARE_CUSTOM="hassan.a:DBG5199"/>
>
<USER NAME="xu.d" HOST="SIM02" IP="172.16.32.12" USED_LICENSES="21000" LOGIN_TIME="2013-05-21 09:33" CHECKOUT_TIME="2013-05-21 09:33" SHARE_CUSTOM="xu.d:SIM02"/>
>
<USER NAME="xu.d" HOST="SIM02" IP="172.16.32.12" USED_LICENSES="6000" LOGIN_TIME="2013-05-21 09:33" CHECKOUT_TIME="2013-05-21 09:33" SHARE_CUSTOM="xu.d:SIM02"/>
>
<USER NAME="xu.d" HOST="SIM02" IP="172.16.32.12" USED_LICENSES="2000" LOGIN_TIME="2013-05-21 09:39" CHECKOUT_TIME="2013-05-21 09:39" SHARE_CUSTOM="xu.d:SIM02"/>
>
<USER NAME="zeiser-rasumak.a" HOST="DBG5200" IP="172.16.225.127" USED_LICENSES="21000" LOGIN_TIME="2013-05-21 10:05" CHECKOUT_TIME="2013-05-21 10:05" SHARE_CUSTOM="zeiser-rasumak.a:DBG5200"/>
>
<USER NAME="xu.d" HOST="DBG4970" IP="172.16.225.126" USED_LICENSES="25000" LOGIN_TIME="2013-05-21 10:25" CHECKOUT_TIME="2013-05-21 10:25" SHARE_CUSTOM="xu.d:DBG4970"/>
>
<USER NAME="zeiser-rasumak.a" HOST="DBG5200" IP="172.16.225.127" USED_LICENSES="21000" LOGIN_TIME="2013-05-21 11:27" CHECKOUT_TIME="2013-05-21 11:27" SHARE_CUSTOM="zeiser-rasumak.a:DBG5200"/>
>
<USER NAME="zeiser-rasumak.a" HOST="DBG5200" IP="172.16.225.127" USED_LICENSES="2000" LOGIN_TIME="2013-05-21 11:48" CHECKOUT_TIME="2013-05-21 11:48" SHARE_CUSTOM="zeiser-rasumak.a:DBG5200"/>
>
<USER NAME="zeiser-rasumak.a" HOST="DBG5200" IP="172.16.225.127" USED_LICENSES="21000" LOGIN_TIME="2013-05-21 11:49" CHECKOUT_TIME="2013-05-21 11:49" SHARE_CUSTOM="zeiser-rasumak.a:DBG5200"/>
</FEATURE>
<FEATURE NAME="HWAIFPBS" VERSION="12.0" VENDOR="ALTAIR" START="2013-03-26" END="2014-03-31" USED_LICENSES="0" TOTAL_LICENSES="2147483647" SHARE="CUSTOM ,VIRTUAL"/>
<FEATURE NAME="HWAWPF" VERSION="12.0" VENDOR="ALTAIR" START="2013-03-26" END="2014-03-31" USED_LICENSES="0" TOTAL_LICENSES="2147483647" SHARE="CUSTOM ,VIRTUAL"/>
4

3 回答 3

1

使用LinqToXml及以下 xml

<?xml version="1.0" encoding="utf-8"?>
<Root>
<FEATURE NAME="f1">
    <USER NAME="u1"/>
    <USER NAME="u2"/>
</FEATURE>

<FEATURE NAME="f2">
    <USER NAME="u3"/>
    <USER NAME="u4"/>
</FEATURE>

</Root>

var xDoc = XDocument.Load(filename);
TreeNode root = new TreeNode();
LoadTree(xDoc.Root.Element("LICENSE_PATH"), root);
treeLic.Nodes.Add(root);

void LoadTree(XElement root,TreeNode rootNode)
{
    foreach (var e in root.Elements().Where(e => e.Attribute("NAME") != null))
    {
        var node = new TreeNode(e.Attribute("NAME").Value);
        rootNode.Nodes.Add(node);
        LoadTree(e, node);
    }
}
于 2013-05-22T10:30:39.010 回答
1
TreeNode rootNode = new TreeNode();
XMLDocument doc = new XmlDocument();
doc.LoadXml(final_Output);
foreach(XmlNode featureNode in doc.DocumentElement.SelectNodes("FEATURE[@NAME]"))
{
   TreeNode fnode = new TreeNode(featureNode.Attributes["NAME"].Value);
   foreach(XmlNode userNode in featureNode.SelectNodes("USER[@Name]"))
   { 
      fNode.Nodes.Add(new TreeNode(userNode.Attributes["NAME"].Value));
   }
   rootNode.Nodes.Add(fnode);
}
myTreeView.Nodes.Add(rootNode);

将是另一种方式。注意我假设 xml 中没有命名空间

于 2013-05-22T10:43:17.503 回答
0

这就是我的方法!

XmlDocument xdoc = new XmlDocument();

                xdoc.Load(ofd.OpenFile());
                tv1.Nodes.Clear();
                AddTreeViewChildNodes(tv1.Nodes, xdoc.DocumentElement);

                MessageBox.Show(ofd.FileName, "filepath")


foreach (XmlNode child_node in xml_node.ChildNodes)
{
    TreeNode new_node = parent_nodes.Add(child_node.Value);
    new_node = parent_nodes.Add(child_node.Name);

    AddTreeViewChildNodes(new_node.Nodes, child_node);

    if (new_node.Nodes.Count == 0) new_node.EnsureVisible();
}

在这里你可以找到一个扩展!

    custom[] customfield = new custom[0]; 

    public Frm()
        InitializeComponent();

    private void c()
    {
        int cu = 0;
        string na = "";
        string fi = "";

        int i = 0;

        dGV_output.RowCount = 1;
        dGV_output.ColumnCount = 3;
        dGV_output.RowHeadersVisible = false;
        dGV_output.ColumnHeadersVisible = false;
        dGV_output.Rows.Add("customernumber", "name", "firsname");

        OpenFileDialog ofd = new OpenFileDialog()
        {
            Filter = "Texts (*.xml)|*.xml|all files (*.*)|*.*",
            Title = ""
        };

        if (ofd.ShowDialog() == DialogResult.OK)
        {
            riTB_ausgabe.Clear();

            XmlTextReader xr = new XmlTextReader(ofd.FileName);

            while (xr.Read()) 
            {
                if (xr.NodeType == XmlNodeType.Element)
                {
                    if (xr.AttributeCount > 0)
                    {
                        while (xr.MoveToNextAttribute())
                        {

                            switch (xr.Name)
                            {
                                case "customernumber":
                                    ku = Convert.ToInt32(xr.Value);
                                    break;
                                case "name":
                                    na = xr.Value;
                                    break;
                                case "firsname":
                                    vo = xr.Value;
                                    break;
                            }

                            riTB_ausgabe.Text +=
                                xr.Name + " -> " + xr.Value + "\n";
                        }
                        riTB_ausgabe.Text += "\n";


                        Array.Resize(ref customfield, customfield.Count() + 1); 
                        customfield[i] = new custom(ku, na, vo); 
                        i++;


                        dGV_ausgabe.Rows.Add(cu, na, fi);

                    }
                }
            }
            xr.Close();
        }
    }

    private void C_add(object sender, EventArgs e)
    {
        if(txt_cnr.Text!="" && txt_name.Text != "" && txt_firstname.Text!="")
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Xml-file(.xml)|*.xml";

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                XmlTextWriter xw = new XmlTextWriter(sfd.FileName, new UnicodeEncoding());

                custom k = new custom(Convert.ToInt16(txt_cnr.Text), Convert.ToString(txt_name.Text), Convert.ToString(txt_firstname.Text));

                Array.Resize(ref customfield, customfield.Count() + 1);
                customfield[customfield.Count() - 1] = k;

                xw.WriteStartDocument();
                xw.WriteStartElement("customerlist");

                foreach (Kunde k1 in customfield)
                {
                    k1.asXmlElementWrite(xw);
                }

                xw.WriteEndElement();
                xw.Close();
            }
        }
        else { MessageBox.Show("error"); }
    }
}
于 2019-11-28T18:31:37.650 回答