Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用具有相同结构的字符串如何将xml文件用于treeview?_C#_Xml_Visual Studio 2010_Parsing_Treeview - Fatal编程技术网

C# 如何使用具有相同结构的字符串如何将xml文件用于treeview?

C# 如何使用具有相同结构的字符串如何将xml文件用于treeview?,c#,xml,visual-studio-2010,parsing,treeview,C#,Xml,Visual Studio 2010,Parsing,Treeview,您好,我如何使用一个字符串,该字符串的结构与xml文件用于treeview的结构相同。 例如,这里是字符串内容的一部分 <?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) 23 hour(s) 0 min(s)

您好,我如何使用一个字符串,该字符串的结构与xml文件用于treeview的结构相同。 例如,这里是字符串内容的一部分

<?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) 23 hour(s) 0 min(s) 3 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"/>
>
....
使用该类, 您可以使用XDocument.Parse(字符串)或XDocument.Load(路径)从字符串创建一个

然后使用以下函数填充树视图(位于):


以下是将xml文档绑定到treeview的示例:

// we cannot bind the TreeView directly to an XmlDocument
// so we must create an XmlDataSource and assign the XML text
XmlDataSource XDdataSource = new XmlDataSource();
XDdataSource.ID = DateTime.Now.Ticks.ToString();  // unique ID is required
XDdataSource.Data = XDoc.OuterXml;

// we want the full name displayed in the tree so 
// do custom databindings
TreeNodeBinding Binding = new TreeNodeBinding();
Binding.TextField = "FullName";
Binding.ValueField = "ID";
TreeView1.DataBindings.Add(Binding);

// Hook up the XmlDataSource to the TreeView
TreeView1.DataSource = XDdataSource;
TreeView1.DataBind();
    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;
            }
        }
    }
}
// we cannot bind the TreeView directly to an XmlDocument
// so we must create an XmlDataSource and assign the XML text
XmlDataSource XDdataSource = new XmlDataSource();
XDdataSource.ID = DateTime.Now.Ticks.ToString();  // unique ID is required
XDdataSource.Data = XDoc.OuterXml;

// we want the full name displayed in the tree so 
// do custom databindings
TreeNodeBinding Binding = new TreeNodeBinding();
Binding.TextField = "FullName";
Binding.ValueField = "ID";
TreeView1.DataBindings.Add(Binding);

// Hook up the XmlDataSource to the TreeView
TreeView1.DataSource = XDdataSource;
TreeView1.DataBind();