Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XML到ListView错误_C#_Xml_Parsing_Listview_Xml Parsing - Fatal编程技术网

C# XML到ListView错误

C# XML到ListView错误,c#,xml,parsing,listview,xml-parsing,C#,Xml,Parsing,Listview,Xml Parsing,我一直在犯错误,看不出我做错了什么 这是密码 private void _FixSave_Offline_Load(object sender, EventArgs e) { System.Xml.XmlDocument NewGame = new System.Xml.XmlDocument(); NewGame.Load(Application.StartupPath + "//Files//Checks_Offline.xml"); foreach (System

我一直在犯错误,看不出我做错了什么

这是密码

private void _FixSave_Offline_Load(object sender, EventArgs e)
{
    System.Xml.XmlDocument NewGame = new System.Xml.XmlDocument();
    NewGame.Load(Application.StartupPath + "//Files//Checks_Offline.xml");

    foreach (System.Xml.XmlNode nameNode in NewGame.SelectNodes("//Games//NewGame"))
    {
        listView1.Items.Add(nameNode.Attributes["Name"].InnerText);
    }
}
这是XML布局

<Games>
 <NewGame>
   <Name></Name>
   <Check></Check>
   <Static></Static>
   <Location></Location>
   <Start></Start>
   <Length></Length>
   <FoundBy></FoundBy>
   <Verified></Verified>
</NewGame>

我不仅尝试过使用“/”,还尝试过使用“/”,所以任何能解决这个问题的方法都是非常受欢迎的,我一辈子都看不出我做错了什么

乍一看,您正在寻找一个名为“name”的属性,但示例中的任何XML元素上都没有属性

我相信您需要名称节点的内容:

foreach (System.Xml.XmlNode nameNode in NewGame.SelectNodes("//Games//NewGame/Name"))
{
    listView1.Items.Add(nameNode.Value);
}

根据XML文档的实际结构,可能需要稍微使用XPath表达式

出于某种原因,我看不到您的XML示例,但请确保您区分了

此外,请确保属性/元素的拼写准确无误。我相信它是区分大小写的

--

编辑:现在我可以查看您的XML了,似乎“Name”实际上是一个元素,而不是一个属性


尝试使用属性,或者使用属性而不是
nameNode.Attributes

我让它工作了。我发现错误是由virtuallist=true引起的。Tim我在上面稍微修改了你的代码,以得到我想要的结果。以下是供任何人用于将来参考的代码。

private void _FixSave_Offline_Load(object sender, EventArgs e) { System.Xml.XmlDocument NewGame = new System.Xml.XmlDocument(); NewGame.Load(Application.StartupPath + "//Files//Checks_Offline.xml"); foreach (System.Xml.XmlNode nameNode in NewGame.SelectNodes("//Games//NewGame/Name")) { listView1.Items.Add(nameNode.InnerText); } }
私有无效\u固定保存\u脱机\u加载(对象发送方,事件参数e) { System.Xml.XmlDocument NewGame=new System.Xml.XmlDocument(); Load(Application.StartupPath+“//Files//Checks_Offline.xml”); foreach(System.Xml.XmlNode Name NewGame.SelectNodes(“//Games//NewGame/Name”)) { listView1.Items.Add(nameNode.InnerText); } } 下面是给定结果的快速屏幕截图


希望这对其他人也有帮助。感谢上面对我的评论,也非常感谢Tim。

nameNode
对象为空还是
nameNode.Attributes[“Name”]
null?如果发生在Attributes集合中,则在当前名为“Name”的节点上找不到属性 private void _FixSave_Offline_Load(object sender, EventArgs e) { System.Xml.XmlDocument NewGame = new System.Xml.XmlDocument(); NewGame.Load(Application.StartupPath + "//Files//Checks_Offline.xml"); foreach (System.Xml.XmlNode nameNode in NewGame.SelectNodes("//Games//NewGame/Name")) { listView1.Items.Add(nameNode.InnerText); } }