根据列表框选择读取XML元素 [英] Read XML element based on list box selection

查看:60
本文介绍了根据列表框选择读取XML元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够基于列表框选择来读取XML元素,并将该数据放入文本框.这是我到目前为止的代码:

I want to be able to read XML elements based on list box selection and have that data placed into a text box. Here is the code I have so far:

Dim xml = XDocument.Load("c:\config.xml")
Dim emp = xml.Descendants("Menu1").FirstOrDefault()

TextBox1.Text = emp.Element(ListBox1.SelectedValue).Value



在我的列表框中,我有一个标记为iNet的项目.我希望能够在列表框中选择iNet,然后在文本框中显示"iNet-QuickGuide".

使用上面的代码时,出现错误对象引用未设置为对象的实例".任何帮助都会很棒.

这是XML文件



In my listbox I have a item labled iNet. I want to be able to select iNet in the listbox and then have "iNet-QuickGuide" show in a text box.

When using the above code I get error "Object reference not set to an instance of an object." Any assistance would be great.

Here is the XML file

<?xml version="1.0" encoding="utf-8" ?>
<!-- XXXX KB Saved Settings - Version 1.0.0.0  -->
<Menu1>
<Box1>
iNet&#13;
Interent Explorer&#13;
Active Directory
</Box1>
<iNet>
iNet-QuickGuide
</iNet>
   <Setting3>true</Setting3>
   <Setting4 />
   <Setting5 />
   <Setting6>false</Setting6>
</Menu1>

推荐答案



您可以使用XMLDocument代替LINQ to XML吗?这是XMLDocument
的示例


Can you use XMLDocument instead of LINQ to XML? Here is an example with XMLDocument

Dim doc As New XmlDocument()

       doc.Load("c:\config.xml")

       Dim n As XmlNode = doc.SelectSingleNode("//Menu1//" + ListBox1.SelectedValue)

       TextBox1.Text = n.InnerText



还请记住,该节点区分大小写. inet<> iNet



also remember that the node is case sensitive. inet <> iNet


对象引用未设置为对象的实例."通常意味着您还没有新"的东西.在这段代码中,您拥有...
"Object reference not set to an instance of an object." usually means that you haven''t "Newed" something. In this bit of code that you have...
Dim xml = XDocument.Load("c:\config.xml")
Dim emp = xml.Descendants("Menu1").FirstOrDefault()



您没有为变量设置数据类型,也没有使用"As New"关键字创建它们的实例.我真的不知道您使用的是哪种数据类型,因此我无法确切告诉您哪些数据类型需要它.您必须研究XDocument以及.Descendants返回的内容,才能找出答案.

***更新***

由于未指定发生错误的行,因此另一种可能性是此行:



You aren''t setting a data type for your variables, and you haven''t created an instance of them with the " As New " keywords. I don''t really know what data types you are using, so I can''t tell you exactly which ones need it. You''ll have to research that the XDocument and whatever the .Descendants returns to find that out.

*** Update ***

Another possibility, since you didn''t specify which line the error occurs on, is this line:

TextBox1.Text = emp.Element(ListBox1.SelectedValue).Value



如果您的错误在此行上,则可能是因为您尚未在ListBox1中选择一个项目,或者它无法在emp.Element中找到该Listbox1.SelectedValue. (这可能是由很多原因引起的...大写或小写或某些内容都需要修整.您必须进行调试才能确定.)



If your error is on this line, then it may be because you don''t have an item selected in ListBox1 yet, or that it can''t find that Listbox1.SelectedValue in the emp.Element. (Which could be caused by many things....upper vs lower case or something needs to be trimmed. You''ll have to debug to know for sure).


我想通了用下面的代码
I figured it out with the below code
Dim doc As New XmlDocument()
        Dim nodes As XmlNodeList
        doc.Load("c:\x\config.xml")
        nodes = doc.SelectNodes("/Menu1/Tertiary/Box3")
        Dim node As XmlNode
        For Each node In nodes
            TextBox1.Text = node.SelectSingleNode(ListBox2.SelectedItem).InnerText
        Next


这篇关于根据列表框选择读取XML元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆