如何从XML文件中获取名称

时间:2013-07-05 22:15:06

标签: xml vb.net

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

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

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

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

如果相机处于<> </>之间,我知道该怎么做,我想我只是因为事情变得复杂,所以我要问:)

2 个答案:

答案 0 :(得分:0)

XPath表达式//Object/@Name将返回所有摄像机名称。

答案 1 :(得分:0)

如果必须处理XML文件,那么您可以做的最好的事情就是依赖XMLReader类。这里有一个如何将它与您的信息一起使用的示例:

    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解析。