0

Currently trying to get a XML file from a location on my machine to display to my Treeview. I pretty much used the code from another stackoverflow question:

Recursion, parsing xml file with attributes into treeview c#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    string Path = Application.StartupPath + @"C:\Users\apearson\Documents\Works.xml";
    public Form1()
    {
        InitializeComponent();
        DisplayTreeView(Path);
    }

    private void DisplayTreeView(string pathName)
    {
        try
        {
            XmlDocument dom = new XmlDocument();
            dom.Load(pathName);

            treeView1.Nodes.Clear();

            foreach (XmlNode xNode in dom.ChildNodes)
            {
                var tNode = treeView1.Nodes[treeView1.Nodes.Add(new TreeNode(xNode.Name))];
                AddNode(xNode, tNode);
            }
        }
        catch (XmlException xmlEx)
        {
            MessageBox.Show(xmlEx.Message);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
    {

        if (inXmlNode is XmlElement)
        {
            foreach (var att in inXmlNode.Attributes.Cast<XmlAttribute>().Where(a => !a.IsDefaultNamespaceDeclaration()))
            {
                inTreeNode.Text = inTreeNode.Text + " " + att.Name + ": " + att.Value;
            }

            var nodeList = inXmlNode.ChildNodes;
            foreach (XmlNode xNode in inXmlNode.ChildNodes)
            {
                var tNode = inTreeNode.Nodes[inTreeNode.Nodes.Add(new TreeNode(xNode.Name))];
                AddNode(xNode, tNode);


            }
        }
        else
        {
            inTreeNode.Text = (inXmlNode.OuterXml).Trim();
        }
        treeView1.ExpandAll();
    }
}

When debugging, I noticed that it will stop at the dom.Load(pathName) then go straight to the catch. Then throw me the error of "The given path's format is not supported". I've seen some other articles with this issue but nothing with a Treeview so don't know if they would have been much help. Is there something I'm missing?

1 Answers1

2

This part

string Path = Application.StartupPath + @"C:\Users\apearson\Documents\Works.xml

... will concatenate the startup path with the full path you define as literal.

This will lead to something like

C:\blah\blub\C:\Users\apearson\Documents\Works.xml

...which is not a valid path..

Shnugo
  • 66,100
  • 9
  • 53
  • 114
  • Ok, so what would be a proper path in this situation? I've tried going with @"\\Works.xml" but I receive every folder and file in my Documents folder instead of just getting the Works.xml file – the1APerson May 08 '18 at 15:54
  • @the1APerson This depends where your file(s) live. If the XML is one file directly within your application's execution path just append the file name to the path. Set a stop mark to `DisplayTreeView(Path);` and check the actual value. This must be the correct full path. You might use relative paths too (`.\SomeDir\SomeFile.xml`), this will use the `current directory` as their starting point. – Shnugo May 09 '18 at 10:21