从xml文件获取信息 [英] getting information from an xml file

查看:85
本文介绍了从xml文件获取信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我今天的另一个问题是...我如何根据我拥有的名称字段读取xml中的ID号.有人告诉我,我需要添加id标签,这很好,但是我很难获得ID号.这个xml文件中,我们只保存指向特定位置的路径,当我需要删除一个路径时,他们想使用id标记删除整个内容,这很好.下面的代码是我用来尝试阅读的代码,但出现错误"

Okay so my other question of the day is... how can i read the ID number in xml based on the name field i have. I was just told at work that i need to add id tags which is fine but i m having the hardest time getting the id number. this xml file we have just holds paths to specific locations and when i need to delete one they want to use the id tag to delete the whole thing which is fine. The code below is what i m using to try and read it but i get the error "

Object reference not set to an instance of an object.

",我所需要的就是获取数字.

这是我的代码:

All i need is to get the number.

Here is my code:

Dim Rinfo = LVapps.SelectedItems(0).Text

xmlDoc.Load(Application.StartupPath & "\Tools.xml")
Dim xmlContactNodeList As XmlNodeList = xmlDoc.GetElementsByTagName("App")
For i = 0 To xmlContactNodeList.Count - 1
    If xmlContactNodeList.Item(i).Item("Name").InnerText = Rinfo Then
        MsgBox(xmlContactNodeList.Item(i).Item("id").InnerText)
    End If
Next




这是xml组的样子:




Here is what the xml group looks like:

- <AdminTools>
- <App id="1">
  <Name>Test2</Name>
  <Path>C:\Users\zachary.shupp\Documents\Tools\Zack's MMC.msc</Path>
  <Icon>1</Icon>
  </App>
  </AdminTools>

推荐答案

您的
MsgBox(xmlContactNodeList.Item(i).Item("id").InnerText)


在名称"节点下没有"id"节点,因此这就是错误的出处.如果您将其更改为


There is no "id" node under the Name node so that is where the error is coming from. If you change it to

MsgBox(xmlContactNodeList.Item(i).Item("Path").InnerText)



您将获得带有路径信息的消息框.

编辑
但是随后我重新阅读了问题,并意识到您想要获取ID. id是一个属性.



You will get the messagbox with the Path information.

Edit
But then I reread the question and realized that you want to get the id. The id is an Attribute.

MsgBox(xmlContactNodeList.Item(1).Attributes(0).OuterXml)


显示一个带有id ="2"


shows a message box with id="2"


对象引用未设置为对象实例的消息框

当您尝试使用属性或调用null/nothing的对象的方法时,会发生此错误.更多详细信息:此处 [xmlContactNodeList.Item(i).Item("id")看起来是Nothing


转到另一部分,找到要尝试访问的值的一种可能方法是使用XPath,请尝试:
Object reference not set to an instance of an object

This error happens when you try to use a property or call a method of an object that is null/nothing. More details: here[^]

A simple use of Visual studio DEBUGGER can tell you the object because of which it is happening. Just look at the stack trace and put a debugger on that line. Check the objects of that line and see if any one is null and you are trying to use that objects property.
For now, either xmlContactNodeList.Item(i).Item("Name") OR xmlContactNodeList.Item(i).Item("id") looks Nothing


Moving to other part, one of the possible way to find the value you are trying to access is using XPath, try:
Dim AdminToolsList As XmlNodeList = ProductXml.SelectNodes("/AdminTools/App/@id")
For Each ToolNode As XmlNode In AdminToolsList 
    MsgBox(ToolNode.InnerText)
Next


您可以尝试以下两种方法之一(请记住,id是一个属性而不是一个元素,因此对它的访问方式有所不同)

(代码未经测试,但应照常工作)

You could try either of these (remember id is an attribute not an element so it is accessed differently)

(Code is untested but should work as is)

Dim appNode As XmlNode = xmlDoc.SelectSingleNode("/AdminTools/App[@id=""" & Identifier & """]")
If Not IsNothing(appNode) Then
'process your node however you wish
End If




或者,如果您有多个具有相同ID的应用程序节点...




Or if you have more than one app node with the same id...

For Each node As XmlNode In xmlDoc.ChildNodes
           If node.HasChildNodes Then
              'Name element is a child of App element
               For Each cnode As XmlNode In node.ChildNodes
                   If cnode.Name = "Name" AndAlso cnode.InnerText = Rinfo Then
                       If node.Attributes.Count > 0 Then
                           For Each attrib As XmlAttribute In node.Attributes
                               If attrib.Name = "id" AndAlso attrib.Value = Identifier Then
                                   'node is the one we want
                               End If
                           Next 'attribute in App node
                       End If
                   End If
               Next 'child element in App node
           End If
       Next 'element in root


这篇关于从xml文件获取信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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