如何从 XML 文件中获取名称 [英] how to get names from an XML file

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

问题描述

我正在制作一个创建批处理渲染脚本的小应用程序,它一切顺利并且完成了它应该做的所有事情,但我遇到了砖墙批处理工具将加密的场景文件转换为仅包含摄像机名称的 XML 文件,因此我要做的是从名为 temp.xml 的文件中检索摄像机名称在 XML 中,它看起来像这样:

I'm making a small application that creates batch rendering scripts, its all going well and does all its supposed to do but I've hit a brick wall The batch tool converts a scene file which is encrypted to XML file with only the camera names, so what I'm trying to do is retrieve the camera names from a file called temp.xml In the XML it looks like this:

<Object Identifier="./Cameras/## Current View ##" Label="Standard Camera" Name="## Current View ##" Type="Camera">

我需要获取 ## 当前视图 ## 和任何其他相机并将它们添加到列表框中

I need to get ## Current View ## and any other camera and add them to a list box

我希望目前这个过程不是模糊的用户输入场景名称,保存路径他们可以手动输入相机名称或按下按钮,通过命令行启动渲染软件加载带有参数的场景(去除所有模型,灯光纹理信息等)并保存一个带有一些渲染选项和相机信息的小xml.. 那个有点工作,但我已经炸了我的大脑哈哈

I hope this isn't to vague at the moment the process is The user enters a scene name, save path they can either enter camera names manually or press a button, which launches the render software by command line loads the scene with arguments (strips out all the model, lights textures info etc) and saves a tiny xml with a few render options and camera info.. that bit works but I've about fried my brain lol

如果相机位于 <></> 我知道该怎么做,我认为我只是把事情复杂化了,所以我问:)

If the camera was in between <> </> I know how to do that I think I'm just over complicating matters hence why I'm asking :)

推荐答案

如果你必须处理一个 XML 文件,你能做的最好的事情就是依赖 XMLReader 类.这里有一个示例,说明如何将其与您的信息结合使用:

If you have to deal with a XML file, the best thing you can do is relying on the XMLReader class. Here you have an example of how to use it with your information:

    Dim path As String = "path of the XML file"
    Dim settings As System.Xml.XmlReaderSettings = New System.Xml.XmlReaderSettings()
    settings.ConformanceLevel = System.Xml.ConformanceLevel.Fragment
    Using reader As System.Xml.XmlReader = System.Xml.XmlReader.Create(path)
        While (reader.Read())
            if (reader.NodeType = System.Xml.XmlNodeType.Element) Then
                If (reader.Name = "Object") Then

                    Dim wholeAttribute As String 'Whole string as contained in the XML attribute
                    Dim betweenHashes As String 'String between #'s


                    'From "Identifier"
                    wholeAttribute = reader.GetAttribute("Identifier")
                    If (wholeAttribute IsNot Nothing And wholeAttribute.Trim.Length > 0) Then
                        If (wholeAttribute.Contains("#")) Then
                            betweenHashes = wholeAttribute.Substring(wholeAttribute.IndexOf("#"), wholeAttribute.LastIndexOf("#") - wholeAttribute.IndexOf("#") + 1)
                            betweenHashes = betweenHashes.Replace("#", "").Trim()
                        Else
                            betweenHashes = wholeAttribute
                        End If
                    End If

                    'From "Name"
                    wholeAttribute = reader.GetAttribute("Name")
                    If (wholeAttribute IsNot Nothing And wholeAttribute.Trim.Length > 0) Then
                        If (wholeAttribute.Contains("#")) Then
                            betweenHashes = wholeAttribute.Replace("#", "").Trim()
                        Else
                            betweenHashes = wholeAttribute
                        End If
                    End If


                    'Adding the string to ListBox1
                    If (betweenHashes IsNot Nothing And betweenHashes.Trim.Length > 0) Then
                        ListBox1.Items.Add(betweenHashes)
                    End If


                End If
            End If
        End While
   End Using

如您所见,上面的代码从两个不同的地方检索您想要的内容.我想这些信息足以帮助您了解如何处理 VB.NET 中的 XML 解析.

As you can see, the code above retrieves what you want from two different places. I guess that this is information more than enough to help you to understand how to deal with the XML parsing in VB.NET.

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

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