Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I dont code that often, and i forget. So instead of hours of googling for something i dont know what is called, i ask a question here with the experts :)

I want to get specific values out of this XML:

<?xml version="1.0" encoding="UTF-16" ?>
<ROOT>
   <TREE Version="01.00" Company="OTHER" StationNr="0111">
      <Device Type="1">
         <Property Type="1">111</Property>
         <Device Type="2" Number="1">
            <Property Type="34">INT</Property>
            <Device Type="3">
               <Device Type="4" Number="1">
                  <Property Type="56">192.168.242.192</Property>
                  <Device Type="29">
                     <Property Type="71">11016938</Property>
				  </Device>
               </Device>
               <Device Type="4" Number="2">
                  <Property Type="56">192.168.242.193</Property>
                  <Device Type="29">
                     <Property Type="71">11016933</Property>
                  </Device>
               </Device>
            </Device>
         </Device>
      </Device>
   </TREE>
</ROOT>


Heres what i am looking for:
I want to look for the property attribute 56 and get the value of 192.168.242.192 when reading.
I also want to loop through property type 2, to find all device type 4.
I also would like to get the value Property Type="71" of this one.

I have a few examples from my past coding, but no XML i have used in the past, looks like this one.

Any idea what to search for?

Kind regards
Kenneth

What I have tried:

I have tried using some old code snipplets and googleing for something i cannot find
Posted
Updated 8-Apr-20 22:03pm
Comments
Maciej Los 9-Apr-20 3:49am    
I do not see "property type 2" in an xml you've posted.
The description of what you want to achieve is not clear. Please, be more specific and provide sample output.

1 solution

Take a look at below example:

VB.NET
Dim xdoc As XDocument = XDocument.Load("FullFileName.xml")
Dim deviceTypes2find As String() = {"1", "4"}
Dim propTypes2find As String() = {"56", "71"}

Dim result = xdoc.Descendants("Property"). _
    Where(Function(x) propTypes2find.Any(Function(a) x.Attribute("Type").Value=a) AndAlso _
        deviceTypes2find.Any(Function(y) x.Parent.Attribute("Type").Value = y)). _
    Select(Function(x) Tuple.Create(x.Parent.Attribute("Type").Value, x.Attribute("Type").Value, x.Value)). _
    ToList()
Console.WriteLine(String.Format("{0}{1}{2}{1}{3}", "DeviceTypeId", Microsoft.VisualBasic.vbTab, "PropertyTypeId", "Value"))
For Each t As Tuple(Of String, String, String) In result
    Console.WriteLine(String.Format("{0}{1}{2}{1}{3}", t.Item1, Microsoft.VisualBasic.vbTab, t.Item2, t.Item3))
Next


Result:
DeviceTypeId PropertyTypeId  Value
4            56              192.168.242.192
4            56              192.168.242.193
 
Share this answer
 
v3
Comments
kjohansen2000 9-Apr-20 4:36am    
Thank you very much, this takes me a very good part of the road and learned me something new!

The files i need to read are actually close to 10.000 rows and a lot of duplicated "type" numbers.
Can you give me a hint to read the file in a hierarchical way?

When reading the file as a human, i see that Device type 1 has a device type 2, and that has a device type 3, that has 2x device type 4. Those Device type 4, has their own type 56, but 56 is also a value in type 1, so i would like to see if the type 56 i read belongs to a type 4 or a type 1, does it make sense?

Sorry for not posting the full file.
Maciej Los 9-Apr-20 4:38am    
Does other devices have nodes with Property types of 56 and 71?
In the example you provided, devices with types of 1,2,3 have no nodes with Property types of 56 and 71...
kjohansen2000 9-Apr-20 4:46am    
Correct, i tried to limit the file to the needed, but maybe it just made it harder for me getting help.

Yes, other devices also has 56 and 71's
Maciej Los 9-Apr-20 5:19am    
See updated answer (again).
kjohansen2000 9-Apr-20 12:38pm    
I owe you a large cold beer in the sun.. THANKS !!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900