1

I have problem with reading xml atributes in for-next loop, atributes have numbers 1,2,3 how resolve it?

example xml :

<nr1>0 445 110 002</nr1> 
<make1>FIAT</make1>
<type1>CRI-1.0 </type1>
<model1>DUCATO 2.2 </model1>

<nr2>0 445 110 007</nr2>
<make2>PSA FIAT</make2>
<type2>CRI-1.0 </type2>
<model2>SCUDO 2.0JTD </model2>

<nr3>0 445 110 007</nr3>
<make3>PSA FIAT</make3>
<type3>CRI-1.0 </type3>
<model3>SCUDO 2.0JTD </model3>



1 Answer 1

1

You can use StartsWith on element name:

Class Car
  Public nr As String
  Public make As String
  Public type As String
  Public model As String
End Class

Sub Main()
  Dim cars = <cars>
               <nr1>0 445 110 002</nr1>
               <make1>FIAT</make1>
               <type1>CRI-1.0 </type1>
               <model1>DUCATO 2.2 </model1>

               <nr2>0 445 110 007</nr2>
               <make2>PSA FIAT</make2>
               <type2>CRI-1.0 </type2>
               <model2>SCUDO 2.0JTD </model2>

               <nr3>0 445 110 007</nr3>
               <make3>PSA FIAT</make3>
               <type3>CRI-1.0 </type3>
               <model3>SCUDO 2.0JTD </model3>
             </cars>

  Dim carsList As New List(Of Car)
  Dim newCar As New Car
  For Each el As XElement In cars.Elements
    If el.Name.ToString.StartsWith("nr") Then
      If newCar.nr IsNot Nothing Then
        carsList.Add(newCar)
        newCar = New Car
      End If
      newCar.nr = el.Value.ToString
    End If
    If el.Name.ToString.StartsWith("make") Then _
      newCar.make = el.Value.ToString
    If el.Name.ToString.StartsWith("type") Then _
      newCar.type = el.Value.ToString
    If el.Name.ToString.StartsWith("model") Then _
      newCar.model = el.Value.ToString
  Next
  carsList.Add(newCar)
End Sub

EDIT: Display all car numbers in one line via comma.

With LINQ:

Dim carNumbersOnly As String =
  String.Join(", ", carsList.Select(Function(car) car.nr))

Without LINQ:

Private Function CarsToString(cars As IEnumerable(Of Car)) As String
  Dim numbersList As New List(Of String)
  For Each c As Car In cars
    numbersList.Add(c.nr)
  Next
  Return String.Join(", ", numbersList)
End Function

Usage:

Dim carNumbersOnly As String = CarsToString(carsList)

You can assign to TextBox.Text instead of a string, the above is for demo purposes only.

Sign up to request clarification or add additional context in comments.

31 Comments

Thanks for help,but this code generates errors in below lines Dim cars = < cars> ... and Dim carsList As New List(Of Car) Dim newCar As New Car
@user3449603: It runs fine for me. carsList gets populated with 3 car objects. Which visual studio are you using? Mine is 2013. Which error are you getting?
I use VS2008,firstly I can't dim cars = <cars> <nr1>0 445 110 002</nr1> etc... it generate errors, I think should be dim cars as ...,or I don't understad it_________also Dim carsList As New List(Of Car) generate error : "type car is not defined"
I made test and if I opened new project in VS2008 , then it works without errors (after little modification), but in existing project this generate many errors (when I paste sub main()...)
@user3449603: It is VB.NET 10 syntax. For big chunks of code, be sure that I wrote in VS first, and verified not only for compile time errors, but also that it runs perfectly and yields expected results. VS 2008 uses version 9, I don't remember if such inline xml assignment was supported in it. Option infer On allows you to skip explicit type declaration. So Dim a = 5 is a perfectly valid statement, a will be of type integer. XDocument/XElement is supported since .NET 3.5. Try doing load from file.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.