简体   繁体   中英

Get value from xml file with VB.net

I need to get the all <symbol> value form a xml file to an array. 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. I created a dictionary that has the ID and the Symbol.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM