Home > Blockchain >  Get "MC machine" values from xml
Get "MC machine" values from xml

Time:10-28

I have more xml file. I need to get the MC machine lines values. What is the simples way to get these values in vb.net? These vaues are all times in "Attr num="123"" block.

<?xml version="1.0" encoding="UTF-8"?>
<RadanCompoundDocument xmlns="http://www.radan.com/ns/rcd">
  <RadanAttributes>
    <Group  name="System" desc="These attributes describe the RADAN system used to create this file."
        ord="8">
      <Attr num="13" name="Language" desc="RADAN language code." type="i" ord="1" value="19">
        <Valid perm="r"/>
      </Attr>
      </Attr>
      <Attr num="15" name="Build" desc="RADAN software build." type="s" ord="3" value="2022.1.2228">
        <Valid perm="r"/>
      </Attr>
    </Group>
    <Group  name="Manufacturing" desc="These attributes are the manufacturing properties of the file."
        ord="6">
      <Attr num="119" name="Material" desc="Material." type="s" ord="1" value="Mild Steel">
        <Valid perm="e" max="100"/>
      </Attr>
      <Attr num="120" name="Thickness" desc="Thickness." type="r" ord="2" value="1">
        <Valid perm="e" min="0" max="99999"/>
      </Attr>
      <Attr num="121" name="Thickness units" desc="Thickness units." type="s" ord="4" value="mm">
        <Valid perm="e" expr="mm|in|swg" max="80"/>
      </Attr>
      <Attr num="123" name="Cycle time" desc="Cycle time in minutes." type="r" ord="26" value="0">
        <Valid perm="e" min="0"/>
        <MC machine="psys_CAA001_1" value="10"/>
        <MC machine="psys_CAA001_2" value="20"/>
        <MC machine="psys_CAA001_3" value="30"/>
        <MC machine="psys_CAA001_4" value="40"/>
      </Attr>
      <Attr num="124" name="Sheet X" desc="Sheet length in the X direction." type="r" ord="12" value="2500">
        <Valid perm="e" min="0"/>
      </Attr>
      <Attr num="125" name="Sheet Y" desc="Sheet length in the Y direction." type="r" ord="13" value="1250">
        <Valid perm="e" min="0"/>
      </Attr>
    </Group>
    </Group> 
    </RadanAttributes>
</RadanCompoundDocument>

Thanks! Tibi

I not work similar xml before.

CodePudding user response:

Use XmlDocument.SelectNodes with a fitting XPath expression to retrieve a list of matching nodes XmlNodeList

Something like

Dim xmlDoc As New XmlDocument
xmlDoc.Load("MyFile.xml")

Dim xmlNodes As XmlNodeList = xmlDoc.SelectNodes("//RadanCompoundDocument/RadanAttributes/Group/Attr/MC")

For i As Int32 = 0 To xmlNodes.Cound -1
   Dim xmlNode As XmlNode = xmlNodes.Item(i)
   ' Do stuff with the individual "MC" node
Next i

CodePudding user response:

Use Xml Serialization

Imports System.Xml
Imports System.Xml.Serialization

Module Module1
    Const FILENAME As String = "c:\temp\test.xml"
    Sub Main()
        Dim reader As XmlReader = XmlReader.Create(FILENAME)
        Dim serializer As XmlSerializer = New XmlSerializer(GetType(RadanCompoundDocument))
        Dim document As RadanCompoundDocument = serializer.Deserialize(reader)
    End Sub


End Module
<XmlRoot(Namespace:="http://www.radan.com/ns/rcd")>
Public Class RadanCompoundDocument
    <XmlArray(ElementName:="RadanAttributes")>
    <XmlArrayItem(ElementName:="Group")>
    Public Group As List(Of Group)
End Class
Public Class Group
    <XmlAttribute(AttributeName:="class")>
    Public xClass As String
    <XmlAttribute(AttributeName:="desc")>
    Public desc As String
    <XmlAttribute(AttributeName:="name")>
    Public name As String

    <XmlElement(ElementName:="Attr")>
    Public attr As List(Of Attr)
End Class
Public Class Attr
    <XmlAttribute(AttributeName:="num")>
    Public num As Integer
    <XmlAttribute(AttributeName:="name")>
    Public name As String
    <XmlAttribute(AttributeName:="desc")>
    Public desc As String
    <XmlAttribute(AttributeName:="type")>
    Public type As String
    <XmlAttribute(AttributeName:="ord")>
    Public ord As Integer
    <XmlAttribute(AttributeName:="value")>
    Public value As String

End Class
  • Related