XML 到 ListView 错误
我一直在犯这个错误,并且看不出我做错了什么。
这是代码
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>
这是我不断收到的错误
并且 Visual Studio 突出显示了以下代码:
listView1.Items.Add(nameNode.Attributes["Name"].InnerText);
我不仅尝试过使用“//”,还尝试过使用“/”,所以任何能解决这个问题的东西都会受到欢迎,因为我一生都看不到我做错了什么。
I keep getting errors with this, and can't see what I'm doing wrong.
Here's the code
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);
}
}
And here is the XML Layout
<Games>
<NewGame>
<Name></Name>
<Check></Check>
<Static></Static>
<Location></Location>
<Start></Start>
<Length></Length>
<FoundBy></FoundBy>
<Verified></Verified>
</NewGame>
Here's is the error I keep getting
and visual studio highlights the following code:
listView1.Items.Add(nameNode.Attributes["Name"].InnerText);
I've tried using not only "//" but also "/" so anything that will fix this will be more than welcome, b/c I can't for the life of me see what I'm doing wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
乍一看,您正在寻找名称为“Name”的属性,但示例中的任何 XML 元素上都没有属性。
我相信您需要 Name 节点的内容:
您可能需要稍微使用一下 XPath 表达式,具体取决于 XML 文档的实际结构。
At a glance, you're looking for an attribute with the name of "Name", but there are no attributes on any of the XML elements in your example.
I believe you want the content of the Name node:
You might have to play with the XPath expression a bit, depending on the actual structure of your XML document.
由于某种原因,我看不到您的 XML 示例,但请确保您区分 元素和属性< /a>
另外,请确保属性/元素准确拼写为“Name”。我相信它区分大小写。
--
编辑:现在我可以查看您的 XML,看来“Name”实际上是一个元素,而不是一个属性。
尝试使用 Item 属性或 Value 属性而不是
nameNode.Attributes
。I couldn't see your XML example for some reason, but make sure you are distinguishing between Elements and Attributes
Also, make sure that the attribute/element is spelled "Name" exactly. I believe it is case sensitive.
--
Edit: Now I am able to view your XML, it appears that "Name" is actually an element, rather than an attribute.
Try using the Item property, or the Value property instead of
nameNode.Attributes
.我让它工作了。事实证明,错误是由于 virtuallist = true 造成的。蒂姆,我对上面的代码进行了一些修改,以获得我想要的结果。这是任何人都可以用于将来参考的代码。
这是给定结果的快速屏幕截图。
希望这对其他人也有帮助。感谢上面对我的评论的人,非常感谢蒂姆。
I got it to work. Turns out that I the error was due to virtuallist = true. Tim I modified your code above just a little to get the result I wanted. Here's the code for anyone to use for future ref.
And here's a quick screenshot for the given result.
Hope this helps others as well. Thanks to the above people who commented me on this, and big thanks to Tim.