帮助编辑XML文件 [英] Help editing XML file

查看:70
本文介绍了帮助编辑XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个xml文件,该文件保存有时需要更新的数据,以下是我的xml文件的示例.我正在尝试这样做,以便我可以编辑4个项目中的任何一个,以防万一我在拼写错误或位置更改时出错.下面是我创建的函数,它将找到名称,但是一旦它碰到PATH,ARG或ICON,我就会收到错误消息:

I have an xml file that hold data that some times needs to be updated, below is an example of my xml file. I am trying to make it so i can edit any of the 4 items just in case i make a mistake in spelling or a location changes. Below is the function i created and it will find the name but once it hits PATH,ARG, or ICON i get the error :

Object reference not set to an instance of an object.



如果有人可以帮助我,那就太好了.



If someone could help me that would be great.

<?xml version="1.0" encoding="utf-8"?>
<!--XML Database.-->
<AdminTools>
  <App>
    <Name>CMD</Name>
    <Path>C:\Windows\System32\cmd.exe</Path>
    <ARG> /k cd "C:\"</ARG>
    <Icon>50</Icon>
  </App>
</AdminTools>







Function EditXML(ByVal sTool As String, ByVal sDisplayName As String, ByVal sLocation As String, ByVal sArg As String, ByVal sIcon As String)
    ' Load the XmlDocument.
    Dim xd As New XmlDocument()
    xd.Load(SynLaunchXML)


    Dim nod As XmlNode = xd.SelectSingleNode("/AdminTools/App/Name[text()='" & sTool & "']")
    If nod IsNot Nothing Then
        nod.ChildNodes(0).InnerText = sDisplayName
        nod.ChildNodes(1).InnerText = sLocation
        nod.ChildNodes(2).InnerText = sArg
        nod.ChildNodes(3).InnerText = sIcon
        xd.Save(SynLaunchXML)
        Return True
        Exit Function
    Else
        Return False
        Exit Function
    End If
End Function

推荐答案

这就是我的工作方式:

This is how I do things like this:

Dim xd As New XmlDocument()
xd.Load(SynLaunchXML)

If xd.DocumentElement IsNot Nothing Then
    For Each Elem As XmlElement In xd.DocumentElement.ChildNodes
        If Elem.Name = "App" Then
            For Each E As XmlElement In Elem.ChildNodes
                Select Case E.Name
                    Case "Name"
                        E.Value= sDisplayName
                    ....
                End Select
            Next
            Exit For
        End If
    Next
End If



打开文档.如果它具有一个文档元素(在您的示例中为AdminTools),请循环浏览其子节点,直到找到一个名为App的子元素.然后循环遍历其子节点并根据元素名称分配值.处理App后,退出外部循环.

这有点罗word,但它的优点是绕过了所有未命名为App的主节点,包括您可能要添加的任何注释节点.它还不依赖于Name和其他子节点处于特定顺序.

另一个可能导致问题的问题是XML对特殊字符 VERY 挑剔.我的规则是,如果节点可能只包含字母数字字符以外的任何内容,则数据将被CDATA包装.特殊字符可能会引起问题,这就是为什么我通常使用Value而不是InnerText的原因:对于Value,框架的内部工作原理将确定是否需要CDATA并执行自动翻译.

添加了:



Open the document. If it has a document element (AdminTools, in your example), cycle through its child nodes until you find the one named App. Then cycle through its child nodes and assign values based on the name of the element. After you have handled App, exit the outer loop.

This is a bit wordy, but has the advantage of bypassing all primary nodes not named App, including any comment nodes you may want to add. It also does not depend on Name and the other child nodes being in a particular order.

One other issue that might cause problems is that XML is VERY picky about special characters. My rule is that if a node might have anything other than strictly alphanumeric characters, the data gets wrapped with CDATA. It is possible that the special characters could be causing problems, which is why I generally use Value rather than InnerText: with Value, the Framework''s inner workings will determine if CDATA is needed or not and do the translation automatically.

Added: This article[^] does a good job of explaining the difference between Value and InnerText, in case you are interested.


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

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