Logic to reading an XML file in vb.net

荒凉一梦 提交于 2020-01-17 01:27:07

问题


I have an XML file that i am trying to read and rewrite (for later manipulation) to a new text file using vb.net.

I am able to read the XML successfully and print it out, but i am having problems getting the correct with then start and end tags of each element/attribute.

I am using an 'xmlNodeReader' to read the document by getting the name and value of each read. Then a select case with xmlNodeType.Element or xmlNodeType.EndElement

How do I get the correct logic to solve this, as some XML lines are in the form <Server Type="PropertyDefinitions"> and others are in the form <Server Type="aServerName"/>

Ive tried a if loop for endEntity among other things but none seemed to work. Here is (part of) my code which shows the read and write function. If I haven't provided enough information, please let me know.

     Dim reader As XmlNodeReader = New XmlNodeReader(document)
     Dim result As New StringBuilder
      While reader.Read
        Select Case reader.NodeType

            Case XmlNodeType.Element
            result.Append("<" & reader.Name)

                    If reader.HasAttributes Then
                        While reader.MoveToNextAttribute()
                            result.Append(" " + reader.Name + "=" + Chr(34) + reader.Value + Chr(34))
                        End While

                        If XmlNodeType.EndEntity Then
                            result.Append("/>")
                        End If
                    Else
                        If XmlNodeType.Entity Then
                            result.Append(">")
                        ElseIf XmlNodeType.EndEntity Then
                            result.Append("/>")
                        End If
                    End If

            Case XmlNodeType.EndElement
                result.Append("</" + reader.Name + ">")

        End Select
    End While

Example sample of the XML which shows the 3 different types of tags:

    <DocumentSMG Version="6.900000" VersionSeemage="6.12.0.2428">
     <Server Type="PropertyDefinitions">
        <MetaProperties>
        </MetaProperties>
     </Server>
      <Server Type="aServerType1">
        <BOM.Sort.Ascendant Value="1"/>
     </Server>
      <Server Type="aServerType2"/>
      <Server Type="aServerType3"/>
    </DocumentSMG>

回答1:


Try this

Imports System.Xml
Imports System.Xml.Linq
Module Module1
    Const FILENAME As String = "c:\temp\test.xml"
    Sub Main()
        Dim reader As XmlReader = XmlReader.Create(FILENAME)

        While Not reader.EOF
            If reader.Name <> "Server" Then
                reader.ReadToFollowing("Server")
            End If
            If Not reader.EOF Then
                Dim server As XElement = XElement.ReadFrom(reader)
                Dim type As String = server.Attribute("Type")
                Select Case type
                    Case "PropertyDefinitions"

                    Case "aServerType1"

                    Case "aServerType2"

                    Case "aServerType3"

                End Select

            End If

        End While



    End Sub

End Module



回答2:


I would recommend you instead try using the XDocument class and Linq for this. Using this XML (XMLFile.xml):

<?xml version="1.0" encoding="utf-8" ?>
<DocumentSMG Version="6.900000" VersionSeemage="6.12.0.2428">
  <Server Type="PropertyDefinitions">
    <MetaProperties>
    </MetaProperties>
  </Server>
  <Server Type="aServerType1">
    <BOM.Sort.Ascendant Value="1"/>
  </Server>
  <Server Type="aServerType2"/>
  <Server Type="aServerType3"/>
</DocumentSMG>

You can read the document like this:

Dim xml = XDocument.Load("XMLFile.xml")

Now I am not sure exactly what you want to extract from the file, but let's say you want Server nodes of a specific type only:

Dim serverNodes = xml...<Server>.Where(Function(node) node.@Type = "aServerType1")

... is an alias for the Descendants property @ denotes an attribute

So the above looks for nodes that are descendants of the document root with a Type attribute containing "aServerType1".

You can also modify values directly in the XDocument, and then save it.

To save the document:

xml.Save("NewXMLFile.xml")


来源:https://stackoverflow.com/questions/37750797/logic-to-reading-an-xml-file-in-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!