-1

I try to load my XML file into tree-view, and I fail to do so. Only I get element name. I want my tree-view load with XML attributes. My question is how to get XML attributes to load my tree-views?

Kind Regards,

MAIN FORM This is my form that Initialize the XML file.

public Form1()
{
    InitializeComponent();

    // Create an instance of the open file dialog box.
    // This is test purpose only. In production xml files will come from SQL Database.
    OpenFileDialog openFileDialog1 = new OpenFileDialog();

    // Set filter options and filter index.
    openFileDialog1.Title = "Please Choose XML File";
    openFileDialog1.Filter = "XML Files (.xml)|*.xml|All Files (*.*)|*.*";
    openFileDialog1.FilterIndex = 1;// varsayılan olarak jpg uzantıları göster
    openFileDialog1.Multiselect = false;

    // Call the ShowDialog method to show the dialog box.
    openFileDialog1.ShowDialog();

    XmlDataDocument xmldoc = new XmlDataDocument();
    XmlNode xmlnode;
    FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
    xmldoc.Load(fs);
    xmlnode = xmldoc.ChildNodes[1];
    treeView1.Nodes.Clear();
    treeView1.Nodes.Add(new TreeNode(xmldoc.DocumentElement.Name));
    TreeNode tNode;
    tNode = treeView1.Nodes[0];
    AddNode(xmlnode, tNode);
}

XML LOAD This is my xml load sub.

        // XML NODE: ADD
        private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
        {
            XmlNode xNode;
            TreeNode tNode;
            XmlNodeList nodeList;
            int i = 0;
            if (inXmlNode.HasChildNodes)
            {
                nodeList = inXmlNode.ChildNodes;
                for (i = 0; i <= nodeList.Count - 1; i++)
                {
                    xNode = inXmlNode.ChildNodes[i];
                    inTreeNode.Nodes.Add(new TreeNode(xNode.Name));
                    tNode = inTreeNode.Nodes[i];
                    AddNode(xNode, tNode);
                }
            }
            else
            {
                inTreeNode.Text = inXmlNode.InnerText.ToString();
            }
        }

This is XML Code:

<?xml version="1.0" encoding="utf-8"?>
<Menu>
  <AgencyType id="1" name="WATER" Active="Y">
    <AgencyCode id="1" name="FRESH" Active="Y">
      <TypeOfBills id="1" name="INTKON" Active="Y">
        <PaymentType id="1" name="AA" Active="Y"></PaymentType>
        <PaymentType id="2" name="BB" Active="N" /></PaymentType>
        <PaymentType id="3" name="CC" Active="N"></PaymentType>
      </TypeOfBills>
    </AgencyCode>
  </AgencyType>
</Menu>

ORGINAL XML FILE This is my "XML Notepad 2007" application that I build my xml files.

My XML File

TREEVIEW XML LOAD This is how my treeview shown on the form, after when I load my xml data.

Main Form Treeview From Xml

NTMS
  • 816
  • 7
  • 22
  • *Please* post code as text rather than pictures. It's much easier to read, and then we can copy and paste. Likewise, post the XML file as text - ideally a short but complete file with a short but complete console app demonstrating the problem. – Jon Skeet Apr 30 '15 at 08:38
  • You could use XmlNode.InnerXml and XmlNode.InnerText properties – Apple Apr 30 '15 at 08:42
  • No you didn't. You haven't shown the XML file as text, and you haven't shown a short but complete console app demonstrating the problem. (The UI part is incidental here, IMO.) I would strongly recommend using LINQ to XML instead of XDocument, btw - it's much cleaner. – Jon Skeet Apr 30 '15 at 08:45
  • Thank you Jon, I forget to add xml as text. There isn't anyting only UI. UI has only Treeview control. – NTMS Apr 30 '15 at 09:18
  • @Apple what do you mean in ( xNode = inXmlNode.ChildNodes[i];) code use the XmlNode.InnerXml? The XmlNode.InnerXml has very long line divided bay back slash. !!! – NTMS Apr 30 '15 at 09:21
  • @Jon, can you give me some more info about "LINQ to XML" and how to adopt in my app? – NTMS Apr 30 '15 at 09:22
  • Rather than repeating what's in hundreds of tutorials, I suggest that you search for those tutorials. https://msdn.microsoft.com/en-us/library/bb387098.aspx is a good starting point. – Jon Skeet Apr 30 '15 at 09:23
  • @Jon, I already have xml files. So can I use LINQ to read from my xml file? I understand I have to build some query (LINQ) for this. My XML file structure is not going to change. Only the attributes might change. So do I have to buil new query if any attributes changes? And do you know any xml file builder app? Kind Regards. – NTMS Apr 30 '15 at 09:38
  • I don't think you've done the research I asked you to. LINQ to XML is a whole XML API - it's not just for querying; it replaces the use of `XmlDocument` etc. – Jon Skeet Apr 30 '15 at 09:54
  • I think I misunderstood your question since the title says how to get innerxml. I think what you need is in this permalink: http://stackoverflow.com/a/1600086/3175822 – Apple Apr 30 '15 at 10:22

2 Answers2

0

Try something like this

        else
        {
            inTreeNode.Text = inXmlNode.InnerText.ToString();
            //add new code here
            XmlNode inXmlNode = null;
            XmlAttributeCollection attributes = null;
            inXmlNode.Attributes.CopyTo(atttributes);
            foreach(XmlAttribute attribute in attributes)
            {
               string name = attribute.Name;
               string value = attribute.Value;
            }
        }
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • Thank you @jdweng. I get only "AgencyType" but not the other child node. – NTMS Apr 30 '15 at 12:43
  • I only put the code in the leaf (no children) and not in the intermediate nodes. I just got the attributes and didn't put the attributes into the tree. Left that for you to do. – jdweng Apr 30 '15 at 14:32
0

Thank you. I made it. Here it is. I used "XElement" and it loads very fast.

Main:

using System.Xml.Linq; 

public Form1()
{
    InitializeComponent();

    // Create an instance of the open file dialog box.
    // This is test purpose only. In production xml files will come from SQL Database.
    OpenFileDialog openFileDialog1 = new OpenFileDialog();

    // Set filter options and filter index.
    openFileDialog1.Title = "Please Choose XML File";
    openFileDialog1.Filter = "XML Files (.xml)|*.xml|All Files (*.*)|*.*";
    openFileDialog1.FilterIndex = 1;
    openFileDialog1.Multiselect = false;

    // Call the ShowDialog method to show the dialog box.
    openFileDialog1.ShowDialog();

    FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);

    var data = XElement.Load(openFileDialog1.FileName);
    TreeNode treeNode = treeView1.Nodes.Add("Menu");
    LoadElements(data, treeNode);

    //Clear ListBox items
    ListBoxMain.Items.Clear();

    //Load ListBox First time
    foreach (TreeNode n in treeView1.Nodes)
    {
            ListBoxMain.Items.Add(n.Text);
    }
}

LOAD ELEMENTS

private void LoadElements(XElement xElem, TreeNode treeNode)
{
    foreach (XElement element in xElem.Elements())
    {
        if (element.HasElements)
        {
            if (element.FirstAttribute != null)
            {
                TreeNode tempNode = treeNode.Nodes.Add(element.FirstAttribute.Value + "." + element.Attribute("name").Value);
                LoadElements(element, tempNode);
            }
            else
            {
                LoadElements(element, treeNode);
            }
        }
        else
            treeNode.Nodes.Add(element.FirstAttribute.Value + "." + element.Attribute("name").Value);
    }
}
NTMS
  • 816
  • 7
  • 22