Instantiating a collection in an Object



I have instantiated an object called objPet based on a class I generated from an XSD schema. The XML contains a repeating section of details of each animal.


How do I get the repeating XML into the objPet object?


I can get the XML into a SEPARATE object called objAnimalDetail based on the subclass PetsAnimal, BUT I DON'T WANT THAT. I want the detail in the objPet object.


How?


Class



Imports System.Xml.Serialization

'
' This source code was auto-generated by xsd, Version=4.0.30319.17929.
'


''' <remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")> _
<System.SerializableAttribute> _
<System.Diagnostics.DebuggerStepThroughAttribute> _
<System.ComponentModel.DesignerCategoryAttribute("code")> _
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)> _
<System.Xml.Serialization.XmlRootAttribute([Namespace]:="", IsNullable:=False)> _
Partial Public Class Pets

Private petNumberField As String

Private locationField As String

Private animalClassField As String

Private animalsField As PetsAnimal()

''' <remarks/>
Public Property PetNumber() As String
Get
Return Me.petNumberField
End Get
Set(value As String)
Me.petNumberField = value
End Set
End Property

''' <remarks/>
Public Property Location() As String
Get
Return Me.locationField
End Get
Set(value As String)
Me.locationField = value
End Set
End Property

''' <remarks/>
Public Property AnimalClass() As String
Get
Return Me.animalClassField
End Get
Set(value As String)
Me.animalClassField = value
End Set
End Property

''' <remarks/>
<System.Xml.Serialization.XmlArrayItemAttribute("Animal", IsNullable:=False)> _
Public Property Animals() As PetsAnimal()
Get
Return Me.animalsField
End Get
Set(value As PetsAnimal())
Me.animalsField = value
End Set
End Property
End Class

''' <remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")> _
<System.SerializableAttribute> _
<System.Diagnostics.DebuggerStepThroughAttribute> _
<System.ComponentModel.DesignerCategoryAttribute("code")> _
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)> _
Partial Public Class PetsAnimal

Private animalNameField As String

Private animalGenderField As String

Private animalTypeField As String

''' <remarks/>
Public Property AnimalName() As String
Get
Return Me.animalNameField
End Get
Set(value As String)
Me.animalNameField = value
End Set
End Property

''' <remarks/>
Public Property AnimalGender() As String
Get
Return Me.animalGenderField
End Get
Set(value As String)
Me.animalGenderField = value
End Set
End Property

''' <remarks/>
Public Property AnimalType() As String
Get
Return Me.animalTypeField
End Get
Set(value As String)
Me.animalTypeField = value
End Set
End Property
End Class


XML



<?xml version="1.0"?>
<Pets>
<PetNumber>3</PetNumber>
<Location>Home</Location>
<AnimalClass>Mammal</AnimalClass>
<Animals>
<Animal>
<AnimalName>Zeus</AnimalName>
<AnimalGender>Male</AnimalGender>
<AnimalType>Cat</AnimalType>
</Animal>
<Animal>
<AnimalName>Jinx</AnimalName>
<AnimalGender>Female</AnimalGender>
<AnimalType>Cat</AnimalType>
</Animal>
<Animal>
<AnimalName>Spider</AnimalName>
<AnimalGender>Female</AnimalGender>
<AnimalType>Dog</AnimalType>
</Animal>
</Animals>
</Pets>


Code



Imports System.IO
Imports System.Xml
Imports System.Xml.XPath
Imports Microsoft.Office.InfoPath

Imports System.Data.SqlClient
Imports System.Xml.Xsl

Imports System.Xml.Serialization
Imports ConvertXMLandBase64toFiles.Pets


Public Class pets1
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim petXML As XmlDocument = New XmlDocument()

petXML.Load("-------- insert path to pets.xml ---------")

Dim objPet As Pets = New Pets()

Dim petPath As String = "/Pets"

If petXML.HasChildNodes Then

For Each petNode As XmlNode In petXML



objPet.PetNumber = petXML.SelectSingleNode(petPath & "/PetNumber").InnerText.ToString()
objPet.Location = petXML.SelectSingleNode(petPath & "/Location").InnerText.ToString()
objPet.AnimalClass = petXML.SelectSingleNode(petPath & "/AnimalClass").InnerText.ToString()

If petXML.SelectSingleNode(petPath & "/Animals").HasChildNodes Then

Dim objAnimalDetail As PetsAnimal = New PetsAnimal()

For Each animal As XmlNode In petXML.SelectSingleNode(petPath & "/Animals")


objAnimalDetail.AnimalName = petXML.SelectSingleNode(petPath & "/Animals/Animal/AnimalName").InnerText.ToString() '''<---- this needs to be assigned to objPet

ltrl.Text = ltrl.Text & "Has Subnodes<br/>"


Next
Else

ltrl.Text = ltrl.Text & "No Subnodes<br/>"

End If
Next
ltrl.Text = ltrl.Text & objPet.Location.ToString
Else
ltrl.Text = ltrl.Text & "No Children<br/>"

End If


End Sub

End Class

No comments:

Post a Comment