question

LooBi avatar image
0 Votes"
LooBi asked JackJJun-MSFT edited

How to get attribute xml from treeview winforms?

There is a xml file.

<?xml version="1.0" encoding="utf-8"?>
<group name="Student">
  <value>A</value>     
  <value>B</value>
</group>

I need to edit the group's name "Student". but the treeview dosen't show attribute.
How can I get the attribute? Thank you for your helping!

The treeview.
193260-image.png


My Code.

  private void button1_Click(object sender, EventArgs e)
  {
      OpenFileDialog dlg = new OpenFileDialog();
      dlg.Filter = "XML Files (*.xml)|*.xml";
      if (dlg.ShowDialog() == DialogResult.OK)
      {
          try
          {
              this.Cursor = Cursors.WaitCursor;
              XmlDocument xDoc = new XmlDocument();
              xDoc.Load(dlg.FileName);
              tvLabel.Nodes.Clear();
              tvLabel.Nodes.Add(new TreeNode(xDoc.DocumentElement.Name));
              TreeNode tNode = new TreeNode();
              tNode = (TreeNode)tvLabel.Nodes[0];
              addTreeNode(xDoc.DocumentElement, tNode);
              tvLabel.ExpandAll();
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
          }
          finally
          {
              this.Cursor = Cursors.Default;
          }
      }
  }
  private void addTreeNode(XmlNode xmlNode, TreeNode treeNode)
  {
      XmlNode xNode;
      TreeNode tNode;
      XmlNodeList xNodeList;
      if (xmlNode.HasChildNodes)
      {
          xNodeList = xmlNode.ChildNodes;
          for (int x = 0; x <= xNodeList.Count - 1; x++)
          {
              xNode = xmlNode.ChildNodes[x];
              treeNode.Nodes.Add(new TreeNode(xNode.Name));
              tNode = treeNode.Nodes[x];
              addTreeNode(xNode, tNode);
          }
      }
      else
          treeNode.Text = xmlNode.OuterXml.Trim();
  }

---------Edited April, 18----------
Let's say.
I have this kind of XMl file or more complicated one.
so I would like to see all attributes.

<?xml version="1.0" encoding="utf-8"?>
<Order name="Student">
  <group name="TES" Caption="A_TES" Display="true">
  <group name="TOR" Caption="B_TOR" Display="true">
  <group name="TPQ" Caption="D_TPQ" Display="false">
  <group name="TEX" Caption="A_TEX" Display="true">
    <item app="TAL" Page="0">ABC</item>
<item app="TAL" Page="0">CBD</item>
...
...
<Order name="Plan">
  <package key="Special" Row="0" Col="0">
    <group key="Special_VB" Row="0" Col="0">
  <item>bing</item> 
  <item>bx</item>

Like this picture.
193700-image.png


dotnet-csharpwindows-forms
image.png (838 B)
image.png (33.0 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered JackJJun-MSFT edited

@LooBi, Welcome to Microsoft Q&A, you could try to use XDocument to get the get attribute value from the xml.

Code:

  public string FileName { get; set; }
     private void button1_Click(object sender, EventArgs e)
     {
         OpenFileDialog dlg = new OpenFileDialog();
         dlg.Filter = "XML Files (*.xml)|*.xml";
         if (dlg.ShowDialog() == DialogResult.OK)
         {
             try
             {
                 this.Cursor = Cursors.WaitCursor;
                 XmlDocument xDoc = new XmlDocument();
                 xDoc.Load(dlg.FileName);
                 FileName= dlg.FileName;
                 tvLabel.Nodes.Clear();
                 tvLabel.Nodes.Add(new TreeNode(xDoc.DocumentElement.Name));
                 TreeNode tNode = new TreeNode();
                 tNode = (TreeNode)tvLabel.Nodes[0];
                 addTreeNode(xDoc.DocumentElement, tNode);
                 tvLabel.ExpandAll();
             }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message);
             }
             finally
             {
                 this.Cursor = Cursors.Default;
             }
         }
     }
    
    
     private void addTreeNode(XmlNode xmlNode, TreeNode treeNode)
     {
         XmlNode xNode;
         TreeNode tNode;
         XmlNodeList xNodeList;
         if (xmlNode.HasChildNodes)
         {
             xNodeList = xmlNode.ChildNodes;
             for (int x = 0; x <= xNodeList.Count - 1; x++)
             {
                 xNode = xmlNode.ChildNodes[x];
                 string nodetext = xNode.Name+": ";
                 Console.WriteLine(nodetext);
                 if(!nodetext.Contains("text"))
                 {
                     if (xNode.Attributes.Count>0)
                     {
                         for (int i = 0; i < xNode.Attributes.Count; i++)
                         {
                             nodetext+=xNode.Attributes[i].Name+" - "+xNode.Attributes[i].Value;
                         }
                     }
                 }
                    
    
    
                 treeNode.Nodes.Add(new TreeNode(nodetext));
                 tNode = treeNode.Nodes[x];
                 addTreeNode(xNode, tNode);
             }
         }
         else
         {
             string nodetext = xmlNode.Name+": ";
             Console.WriteLine(nodetext);
             if (!nodetext.Contains("text"))
             {
                 if (xmlNode.Attributes.Count>0)
                 {
                     for (int i = 0; i < xmlNode.Attributes.Count; i++)
                     {
                         nodetext+=xmlNode.Attributes[i].Name+" - "+xmlNode.Attributes[i].Value;
                     }
                 }
             }
             else
             {
                 nodetext=xmlNode.InnerText;
             }
             treeNode.Text = nodetext;
         }
            
     }

Result:

193779-image.png


Best Regards,
Jack



If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.




image.png (4.7 KiB)
image.png (16.6 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you so much your answer!
But I wanted diffrenet way.
I updated my question.
Would you check again please? Thank you!

0 Votes 0 ·

@LooBi, I have updated my answer and you could check if it works for you.

0 Votes 0 ·