Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've asked a question some time ago, and got help, but the requirements have changed and i need more :)


The original question:

Quote:
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
<?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?

This was the help i got, and it worked like a charm

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


But now i need more:

Below <device type="1"> i have several <device type="4" number="1">
The following, usually, has an increasing value in "Number", this is the value i need.

Can i get this value, using the solution provided in the past?

What I have tried:

I have tried modifying the code, but without luck..
Posted
Updated 29-Dec-21 9:07am
v2

1 solution

You have to change the value of deviceTypes2find only :)
VB.NET
Dim deviceTypes2find As String() = {"4", "29"}


After that, the result of code (provided by me in the answer to your previous question) is:
DeviceTypeId  PropertyTypeId  Value
4  56  192.168.242.192
29  71  11016938
4  56  192.168.242.193
29  71  11016933


Good luck!

:)
 
Share this answer
 
Comments
kjohansen2000 30-Dec-21 5:15am    
Thanks Maciej,

don't i have to change something in this, looking for "number" ?
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()
kjohansen2000 30-Dec-21 6:40am    
I got your reply on the original question:

"Sorry, i was busy. You have to use the same logic...
This: x.Parent.Attribute("Type").Value extracts "Type" atribute. To get "Number" attribute, use: x.Parent.Attribute("Number").Value
:)"

I don't know how to implement this, I actually tried it, but don't really understand the logic.. :(
Maciej Los 3-Jan-22 15:28pm    
Sorry, i don't understand you. Can you, please, what do you want to achieve? Does the solution provided in my answer resolve your issue, or not?

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