正在读取 XML 文件元素和之间的值
Reading XML File Element and the value between
我已经搜索过,但找不到我要找的东西...
我想获取所有元素并显示(如果有的话)标签中的内容 ?
示例 xml 标签:
<username>pavle_stoj</username>
控制台输出:
username : pavle_stoj
我无法解决问题?
Imports System.Xml
Module Module1
Sub Main()
If IO.File.Exists("C:\Users\Pavle\Desktop\config.xml") Then
Dim XmlReader As XmlReader = XmlReader.Create("C:\Users\Pavle\Desktop\config.xml")
With XmlReader
While .Read()
If .NodeType = XmlNodeType.Element Then
If .IsStartElement() Then
Console.WriteLine("Element Name: " + .Name)
'Value Of Element: -> ???
End If
End While
End With
Console.Write(".. press any key ..")
Console.ReadLine()
Else
Console.WriteLine("The filename you selected was not found.")
End If
End Sub
模块结束
XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<configfile>
<database_settings>
<databasename>peopledb</databasename>
<databaseuser>sa</databaseuser>
<databasepassword>verysecurepassword</databasepassword>
</database_settings>
<domain_admin_settings>
<username>myusername</username>
<password>verysecurepassword</password>
<domain>windowstest.local</domain>
</domain_admin_settings>
</configfile>
我根据你的描述做了测试,下面的代码对我有效:
Sub Main()
Dim path As String = "your xml path"
If IO.File.Exists(path) Then
Using reader As XmlReader = XmlReader.Create(path)
reader.MoveToContent()
While reader.Read()
If reader.NodeType = XmlNodeType.Element Then
If reader.Name = "username" Then
Dim el As XElement = TryCast(XNode.ReadFrom(reader), XElement)
If el IsNot Nothing Then
Console.WriteLine(el.Value)
End If
End If
End If
End While
End Using
Console.Write(".. press any key ..")
Console.ReadLine()
Else
Console.WriteLine("The filename you selected was not found.")
End If
End Sub
我的测试结果。
使用 XElement...
Dim path As String = "your path here"
Dim xe As XElement
' xe = XElement.Load(path) 'Production
' for TESTING use literal. Get rid of this for production
xe = <configfile>
<database_settings>
<databasename>peopledb</databasename>
<databaseuser>sa</databaseuser>
<databasepassword>verysecurepassword</databasepassword>
</database_settings>
<domain_admin_settings>
<username>myusername</username>
<password>verysecurepassword</password>
<domain>windowstest.local</domain>
</domain_admin_settings>
</configfile>
Dim s As String = String.Format("Element name: {0} Value: {1}",
xe.<domain_admin_settings>.<username>.First.Name,
xe.<domain_admin_settings>.<username>.Value)
Console.WriteLine(s)
制作看起来像这样
Dim path As String = "your path here"
Dim xe As XElement
xe = XElement.Load(path) 'Production
Dim s As String = String.Format("Element name: {0} Value: {1}",
xe.<domain_admin_settings>.<username>.First.Name,
xe.<domain_admin_settings>.<username>.Value)
Console.WriteLine(s)
我已经搜索过,但找不到我要找的东西... 我想获取所有元素并显示(如果有的话)标签中的内容 ?
示例 xml 标签:
<username>pavle_stoj</username>
控制台输出:
username : pavle_stoj
我无法解决问题?
Imports System.Xml
Module Module1
Sub Main()
If IO.File.Exists("C:\Users\Pavle\Desktop\config.xml") Then
Dim XmlReader As XmlReader = XmlReader.Create("C:\Users\Pavle\Desktop\config.xml")
With XmlReader
While .Read()
If .NodeType = XmlNodeType.Element Then
If .IsStartElement() Then
Console.WriteLine("Element Name: " + .Name)
'Value Of Element: -> ???
End If
End While
End With
Console.Write(".. press any key ..")
Console.ReadLine()
Else
Console.WriteLine("The filename you selected was not found.")
End If
End Sub
模块结束
XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<configfile>
<database_settings>
<databasename>peopledb</databasename>
<databaseuser>sa</databaseuser>
<databasepassword>verysecurepassword</databasepassword>
</database_settings>
<domain_admin_settings>
<username>myusername</username>
<password>verysecurepassword</password>
<domain>windowstest.local</domain>
</domain_admin_settings>
</configfile>
我根据你的描述做了测试,下面的代码对我有效:
Sub Main()
Dim path As String = "your xml path"
If IO.File.Exists(path) Then
Using reader As XmlReader = XmlReader.Create(path)
reader.MoveToContent()
While reader.Read()
If reader.NodeType = XmlNodeType.Element Then
If reader.Name = "username" Then
Dim el As XElement = TryCast(XNode.ReadFrom(reader), XElement)
If el IsNot Nothing Then
Console.WriteLine(el.Value)
End If
End If
End If
End While
End Using
Console.Write(".. press any key ..")
Console.ReadLine()
Else
Console.WriteLine("The filename you selected was not found.")
End If
End Sub
我的测试结果。
使用 XElement...
Dim path As String = "your path here"
Dim xe As XElement
' xe = XElement.Load(path) 'Production
' for TESTING use literal. Get rid of this for production
xe = <configfile>
<database_settings>
<databasename>peopledb</databasename>
<databaseuser>sa</databaseuser>
<databasepassword>verysecurepassword</databasepassword>
</database_settings>
<domain_admin_settings>
<username>myusername</username>
<password>verysecurepassword</password>
<domain>windowstest.local</domain>
</domain_admin_settings>
</configfile>
Dim s As String = String.Format("Element name: {0} Value: {1}",
xe.<domain_admin_settings>.<username>.First.Name,
xe.<domain_admin_settings>.<username>.Value)
Console.WriteLine(s)
制作看起来像这样
Dim path As String = "your path here"
Dim xe As XElement
xe = XElement.Load(path) 'Production
Dim s As String = String.Format("Element name: {0} Value: {1}",
xe.<domain_admin_settings>.<username>.First.Name,
xe.<domain_admin_settings>.<username>.Value)
Console.WriteLine(s)