我如何获得treeview`的xml参数值?

时间:2013-05-21 12:43:57

标签: c# xml parsing treeview treeviewitem

嗨,我有一个xml,我希望它在C#

的树视图中显示

它有效,但树视图没有告诉我我想要看到的东西。

我试着解释我的问题:

这是xml:

<?xml version="1.0" encoding="UTF-8"?>
<LM-X STAT_VERSION="3.32">
<LICENSE_PATH TYPE="NETWORK" HOST="6200@lwserv005" 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="LWSERV171" 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"/>
...
</LICENSE_PATH>
</LM-X>

我想在树视图中找到一个结构:这是FEATUREs

GlobalZoneEU HWAIFPBS HWAWPF ...

这是我的c#代码:

private void btnShowLicstate_Click(object sender, EventArgs e)
{
    string command = "\"C:\\lmxendutil.exe\" -licstatxml -host lwserv005 -port 6200";

    string output = ExecuteCommand(command);
    string final_output = output.Substring(90, output.Length-90);

    XDocument doc = XDocument.Parse(final_output);

    BuildTree(treeLic, doc); 


    txtOutput.Text = final_output; 
}

static string ExecuteCommand(string command)
{
    int exitCode;
    ProcessStartInfo processInfo;
    Process process;

    processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
    processInfo.CreateNoWindow = true;
    processInfo.UseShellExecute = false;
    processInfo.RedirectStandardError = true;
    processInfo.RedirectStandardOutput = true;

    process = Process.Start(processInfo);

    string output = process.StandardOutput.ReadToEnd();
    exitCode = process.ExitCode;

    process.Close();

    return output; 
}

private void BuildTree(TreeView treeView, XDocument doc)
{
    TreeNode treeNode = new TreeNode(doc.Root.Name.LocalName);
    treeView.Nodes.Add(treeNode);
    BuildNodes(treeNode, doc.Root);
}

private void BuildNodes(TreeNode treeNode, XElement element)
{
    foreach (XNode child in element.Nodes())
    {
        switch (child.NodeType)
        {
            case XmlNodeType.Element:
                XElement childElement = child as XElement;
                TreeNode childTreeNode = new TreeNode(childElement.Name.LocalName);
                treeNode.Nodes.Add(childTreeNode);
                BuildNodes(childTreeNode, childElement);
                break;
            case XmlNodeType.Text:
                XText childText = child as XText;
                treeNode.Nodes.Add(new TreeNode(childText.Value));
                break;
        }
    }
}

如何获取节点的名称,例如---&gt;

<FEATURE NAME="GlobalZoneEU" VERSION="12.0" VENDOR="ALTAIR"  ...>

我只想要NAME ......

1 个答案:

答案 0 :(得分:1)

完全确定你想要什么,但是你在寻找

(string)childElement.Attributes("NAME")

而不是

childElement.Name.LocalName

相关问题