0

Summary:I want to create objects in a for each loop.

I have a class - Dashboard, which has some properties - length, height, etc.

This info is contained within an XML document, but my class' properties is only a small subset of the information in the XML.

I have created a Collection of XElement, and I can iterate over these, but how do I create my object on each iteration of the loop?

Dim Col_DashBoards As IEnumerable(Of XElement) = From XDashboard In XDocument.Load(filename).Descendants("dashboard")
        For Each XDashboard In Col_DashBoards
            'PseudoCode
        Dim Xdashboard.Name as New DashboardClassObject
            Xdashboard.Name.Height = XDashboard.Element("Height").value 
            ...
        Next
ThomasRones
  • 657
  • 8
  • 29
  • Is there a 1 to many (1..*) relationship within your XML to the class you want to create? – IronAces Jun 19 '18 at 14:53
  • If i understand you correctly, No. There are an unknown number of XElements and they each have a unique name attribute. 1 XML Element will become 1 object – ThomasRones Jun 19 '18 at 14:55
  • That sounds like it is a 1 to many relationship? How many `DashboardClassObject` would you expect to create in the above code? What is the loop for otherwise? – IronAces Jun 19 '18 at 14:56
  • I am opening an XML file that is created via another program, so I dont know how many objects there will be. I could do something like a .Count(), but that would be a runtime thing.... But to answer, there will probably be somewhere between 0 and 50 – ThomasRones Jun 19 '18 at 14:59
  • Okay... So you'd like to create a collection of `DashboardClassObject`? – IronAces Jun 19 '18 at 15:02
  • Exactly, but how do i create the individual objects in the collection? Dim (I Cant Explicitly put the name here b/c it is based on the XML and different in each iteration) as New DashbaordClassObject – ThomasRones Jun 19 '18 at 15:03
  • Did this resolve your question? – IronAces Jun 20 '18 at 08:54
  • Been caught up with other projects, I’ll test it when I have the chance. I think so. I guess I was just confused because I was thinking about it backwards. I wanted to instantiate each object rather than the list of objects. Would it be correct to say that the list instantiates the objects inside the list? – ThomasRones Jun 20 '18 at 08:59
  • You could write it as so, but they way it has been answered the object is instantiated and then added to the list. – IronAces Jun 20 '18 at 09:01

1 Answers1

0

If I have understood your question correctly, you wish to create a new object based on a subset of data from within an XML document?

The below is a function that will generate a new DashboardClassObject for every matching node and populate that object. A list of type DashboardclassObject is returned.

Public Function GenerateDashBoardFromXML(filename as string) As List(Of DashboardClassObject)
   Dim dashboardList As List(Of DashboardClassObject) = New List(Of DashboardClassObject)()
   Dim Col_DashBoards As IEnumerable(Of XElement) = From XDashboard In XDocument.Load(filename)?.Descendants("dashboard")

   For Each XDashboard In Col_DashBoards
       Dim dashboard As DashboardClassObject = New DashboardClassObject () With {
           .Name = XDashboard.Element("Height")?.value 
       }
       dashboardList.Add(dashboard)
   Next

   return dashboardList
End Function

It should be noted that Null checking is used here. The below is only populated if the matching element is found. Null coalescing operator is also an option here.

.Name = XDashboard.Element("Height")?.value 
IronAces
  • 1,857
  • 1
  • 27
  • 36