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?