尝试将 XML 拉入树视图时出错"The given path's format is not supported"



当前正在尝试从计算机上的某个位置获取XML文件以显示到我的树视图。 我几乎使用了另一个堆栈溢出问题中的代码:

递归,将带有属性的 xml 文件解析为树视图 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:UsersapearsonDocumentsWorks.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();
}
}

调试时,我注意到它会在 dom 处停止。加载(路径名(然后直接转到捕获。 然后给我扔"不支持给定路径的格式"的错误。我已经看过其他一些关于这个问题的文章,但没有关于树视图的文章,所以不知道它们是否会有很大帮助。 我错过了什么吗?

这部分

string Path = Application.StartupPath + @"C:UsersapearsonDocumentsWorks.xml

。将启动路径与您定义为文本的完整路径连接起来。

这将导致类似

C:blahblubC:UsersapearsonDocumentsWorks.xml

。这不是一个有效的路径。

相关内容

最新更新