[英]Get value from xml file with VB.net
I need to get the all <symbol>
value form a xml file to an array.我需要将所有<symbol>
值从 xml 文件中获取到数组中。 What is the simplest way to it?最简单的方法是什么?
<?xml version="1.0" encoding="UTF-8"?>
<RadanProject xmlns="http://www.radan.com/ns/project">
<ProjectNestNameTemplate>*</ProjectNestNameTemplate>
<JobName>teszt2222</JobName>
<FirstNestNumber>1</FirstNestNumber>
<Parts>
<NextID>3</NextID>
<Part
<ID>1</ID>
<Symbol>C:\Users\VEREST\Desktop\ide2\kor100.sym</Symbol>
<Kit>-</Kit>
</Part>
<Part>
<ID>2</ID>
<Symbol>C:\Users\VEREST\Desktop\ide2\korteszt.sym</Symbol>
<Kit>-</Kit>
</Part>
</Parts>
<Sheets>
<NextID>1</NextID>
</Sheets>
</RadanProject>
Best Regards, Tibi最好的问候,蒂比
Using Xml Linq.使用 Xml Linq。 I created a dictionary that has the ID and the Symbol.我创建了一个包含 ID 和符号的字典。
Imports System.Xml
Imports System.Xml.Linq
Module Module1
Dim FILENAME As String = "c:\temp\test.xml"
Sub Main()
Dim doc As XDocument = XDocument.Load(FILENAME)
Dim ns As XNamespace = doc.Root.GetDefaultNamespace
Dim dict = doc.Descendants(ns + "Part") _
.GroupBy(Function(x) CType(x.Element(ns + "ID"), String), Function(y) CType(y.Element(ns + "Symbol"), String)) _
.ToDictionary(Function(x) x.Key, Function(y) y.FirstOrDefault())
End Sub
End Module
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.