0

hi i have problems with xml and the treeview control in C# Windows Forms.

My Application:

  1. First I create a string out of a cmd command. This is a output in xml format. the final_output is my string for the XMLReader.

    string command = "\"C:\lmxendutil.exe\" -licstatxml -host lwserv005 -port 6200";

            string output = ExecuteCommand(command);
            string final_output = output.Substring(90, output.Length - 90);
    
  2. Second I use the XMLReader and StringReader for read this string and parse this in xml. than I make a collection with all Nodes with the name "FEATURE" with the attribute NAME="...".

    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. Now I show my list in the TreeView.

    TreeNode node;

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

The XML Structure is how this:

<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="" ...>

... 

I want to get this structure for my treeview with the value of attribute NAME for FEATURE and USER. My treeview show me only the FEATURE with NAME attribute Value. I try it with readsubtree but it don't work :(.

My Question is now...

How I can show alle nodes with the attribute NAME in the treeview and if I click oin a treeview node I can make a Event but how I can use this for show me more informations about the subtree node of the FEATURE node?

xml example:

<?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"/>
Tarasov
  • 3,625
  • 19
  • 68
  • 128

3 Answers3

1

Using LinqToXml and below 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);
    }
}
I4V
  • 34,891
  • 6
  • 67
  • 79
  • the only problem is that i get rootNode = NUll :/ – Tarasov May 22 '13 at 11:24
  • @Tarasov without knowing your *real* xml, it is hard to say anything. But above code works without any problem with the xml I posted. – I4V May 22 '13 at 11:35
  • @Tarasov I changed the line `LoadTree(xDoc.Root.Element("LICENSE_PATH"), root);` and now it works for your xml. – I4V May 22 '13 at 12:32
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);

would be another way. NB I've assumed no namespaces in the xml

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39
0

that's my approach!

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();
}

Here you can find an extension!

    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"); }
    }
}
  • That's the class – Williams_xml Nov 28 '19 at 18:35
  • class Kunde private int knr; private string name; private string fname; public int Cnr { get { return this.cnr; } private set { this.cnr = value; } } public string Name { get { return this.name; } private set { this.name = value; } } – Williams_xml Nov 28 '19 at 18:42