简体   繁体   中英

c# to read xml file and update the checkboxes with the nodes

I have an xml file like this:

<servers>
<general name="1">      
        <service name="ser1"/>
        <service name="ser2"/>  
</general>
<general name="2">      
        <service name="ser1"/>
        <service name="ser2"/>  
</general>
</servers>

In my winform application, I have a treeview list with checkbox property set to true.What I am trying to achieve is that I am attempting to read this xml file and update both the parent and child node to this tree view.

What I have tried is:

XDocument doc = XDocument.Load(@"D:\\path.xml");
        TreeNode node;            
            var gnrl = from general in doc.Descendants("general")
                       select new
                       {
                           parent = general.Attribute("name").Value,
                           child = general.Descendants("service")
                       };
            //Loop through results
            foreach (var general in gnrl)
            {
                 // Add a root node.
            node = dcselectview.Nodes.Add(String.Format(general.parent));
                foreach (var ser in general.child)
                {
                        // Add a node as a child of the previously added node.
                        node = node.Nodes.Add(String.Format(ser.Attribute("name").Value));                        
                }
           }

it reads the file and all details are updated but not in a proper way. rather it is shown as below:

在此处输入图片说明

Needed:

I want the parent element to be on top and down-right to it,the child elements. If possible, it would be nice if I dont have checkboxes for parent elements.

Any help would be really appreciated..

EDIT:

My code edited. Now I am getting as shown in new picture below:

在此处输入图片说明

I want the 2 black lines to be in same line,not as child node of another..

Do you want a hierarchical structure, like this?

在此处输入图片说明

If so, I recommend you to look at the Treeview: http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.checkboxes.aspx

You have to add parent as a node first

public static bool LoadNodesFromXML()
    {
        XDocument doc = XDocument.Load(@"D:\\path.xml");
        var root = doc.Root;
        var childenode = dcselectview.Nodes.Add(root.Attribute("Name").Value);
        foreach (var xElement in root .Elements())
                {
                    InsertNode(childenode, xElement);
                }
}


private void InsertNode(TreeNode parent, XElement element)
        {
            var childenode = parent.Nodes.Add(element.Attribute("Name").Value);
            if(element.Elements().Count() > 0)
                foreach (var xElement in element.Elements())
                {
                    InsertNode(childenode, xElement);
                }

        }

Try this you will get

        XDocument doc = XDocument.Load(@"D:\\test.xml");

        IEnumerable<XElement> Xele = doc.XPathSelectElements("//general");

        foreach (XElement xe in Xele.Descendants())
        {
            //MessageBox.Show(xe.Attribute("name").Value);
        dcselectview.Parent.Text =xe.Parent.Attribute("name").Value; // here parent  value ---->  name="1" and name="2" 
        dcselectview.Nodes.Add(xe.Attribute("name").Value); // ser1     ser2        ser1        ser2
        }

Try this:

public static class TreeViewExtension
{
    public static bool LoadNodesFromXML(this TreeView tv, string xml)
    {
        try
        {
            XDocument doc = XDocument.Parse(xml);
            TreeNode rootNode = new TreeNode();
            rootNode.Text = doc.Root.ToString().Split('>')[0] + ">";
            rootNode.LoadTreeNodes(doc.Root.Elements());
            tv.Nodes.Add(rootNode);
            return true;
        }
        catch { return false; }
    }
    public static void LoadTreeNodes(this TreeNode parentNode, IEnumerable<XElement> elements)
    {
        foreach (var e in elements) {
            TreeNode childNode = new TreeNode();                
            childNode.Text = e.ToString().Split('>')[0] + ">";
            parentNode.Nodes.Add(childNode);
            childNode.LoadTreeNodes(e.Elements());
        }
    }
}
//Usage:
var yourInputXMLString = "<servers><general name=\"1\"><service name=\"ser1\"/>" +
                         "<service name=\"ser2\"/></general><general name=\"2\">" +
                         "<service name=\"ser1\"/><service name=\"ser2\"/>" +
                         "</general></servers>";
treeView1.LoadNodesFromXML(yourInputXMLString);

在此处输入图片说明

Thanks all for your help::But I have found another solution of my own::

XDocument doc = XDocument.Load(@"path\\test.xml");            

        // Add nodes to treeView1.
        TreeNode pnode;
        TreeNode cnode;
            var gnrl = from general in doc.Descendants("general")
                       select new
                       {
                           parent = general.Attribute("name").Value,
                           child = general.Descendants("service")
                       };
            //Loop through results
            foreach (var general in gnrl)
            {
                 // Add a root node.
            pnode = treeview.Nodes.Add(String.Format(general.parent));
                foreach (var ser in general.child)
                {
                // Add a node as a child of the previously added node.
                cnode = pnode.Nodes.Add(String.Format(ser.Attribute("name").Value));                        
                }
           }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM