在vb.net中读取XML文件的逻辑 [英] Logic to reading an XML file in vb.net
问题描述
我有一个XML文件,我正在尝试使用vb.net读取并重写(以便以后操作)到新的文本文件。
I have an XML file that i am trying to read and rewrite (for later manipulation) to a new text file using vb.net.
我能够成功读取XML并将其打印出来,但是我在使用每个元素/属性的开始和结束标记时遇到问题。
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.
我正在使用'xmlNodeReader '通过获取每个读取的名称和值来阅读文档。
然后一个选择案例 xmlNodeType.Element
或 xmlNodeType.EndElement
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
如何获得正确的逻辑来解决这个问题,因为一些XML行的形式是< Server Type =PropertyDefinitions>
和其他的格式为< Server Type =aServerName/>
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"/>
我尝试了一个if循环 endEntity
除其他外,似乎都没有效果。这是我的代码的一部分,它显示了读写函数。
如果我没有提供足够的信息,请告诉我。
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
显示3种不同类型的XML的示例示例标签:
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>
推荐答案
试试这个
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
这篇关于在vb.net中读取XML文件的逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!