Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 215
  • Last Modified:

create a well formed xml in vb.net

I have  two arrays the values of which need to be filled in the xml below
arrname whose length is 6
and arraddress whose legth is 15

An XML format like this is already defined




<name>
   <first name><first name>
   <LastName>LastName>
   <middlename><middlename>
</name>
<Address>
   <street1></<street1>
   <street2></street2>
    <city></city>
</Address>


I need  to get a final xml like the following How would I do this
<name>
   <first name>arrname(5) </first name>
   <LastName>arrname(0) </LastName>
   <middlename>arrname(2) </middlename>
</name>
<Address>
   <street1>arraddress(7)</<street1>
   <street2>arraddress (15)</street2>
    <city>arraddress(10)</city>
</Address>
0
YRKS
Asked:
YRKS
  • 5
  • 3
1 Solution
 
Bob LearnedCommented:
1) Are you asking how to create the XmlDocument and add elements to it from an array?

2) Do you have control over the format of the XML?

3) I would use something like this:

<Name first="" last="" middle="" />
<Address street1="" street2="" city="" state="" />

Bob
0
 
YRKSAuthor Commented:
Yes I do have control over the XML.
YRKS
0
 
Bob LearnedCommented:
Ok, that answers #2, how about #1?

Bob
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
YRKSAuthor Commented:
yes I am asking how to add elements to an xml document from an array
0
 
Bob LearnedCommented:
Here is an untested example:

Imports System.Xml

Public Class Person

    Public Shared Sub SaveFromArray(ByVal fileName As String, ByVal arrName() As String, ByVal arrAddress() As String)

        Dim document As New XmlDocument()

        ' <Person>
        Dim elementPerson As XmlElement = document.CreateElement("Person")

        ' <Name>
        Dim elementName As XmlElement = document.CreateElement("Name")

        ' Add <Name> as child element to <Person>
        elementPerson.AppendChild(elementName)

        ' <FirstName>
        Dim elementFirstName As XmlElement = document.CreateElement("FirstName")
        Dim textFirstName As XmlText = document.CreateTextNode(arrName(5))
        elementFirstName.AppendChild(textFirstName)
        elementName.AppendChild(elementFirstName)

        ' <LastName>
        Dim elementLastName As XmlElement = document.CreateElement("LastName")
        Dim textLastName As XmlText = document.CreateTextNode(arrName(0))
        elementLastName.AppendChild(textLastName)
        elementName.AppendChild(elementLastName)

        ' <MiddleName>
        Dim elementMiddleName As XmlElement = document.CreateElement("MiddleName")
        Dim textMiddleName As XmlText = document.CreateTextNode(arrName(2))
        elementMiddleName.AppendChild(textMiddleName)
        elementName.AppendChild(elementMiddleName)

        ' <Address>
        Dim elementAddress As XmlElement = document.CreateElement("Address")

        ' <Street1>
        Dim elementStreet1 As XmlElement = document.CreateElement("Street1")
        Dim textStreet1 As XmlText = document.CreateTextNode(arrAddress(7))
        elementStreet1.AppendChild(textStreet1)
        elementAddress.AppendChild(elementStreet1)

        ' <Street2>
        Dim elementStreet2 As XmlElement = document.CreateElement("Street2")
        Dim textStreet2 As XmlText = document.CreateTextNode(arrAddress(15))
        elementStreet2.AppendChild(textStreet2)
        elementAddress.AppendChild(elementStreet2)

        ' <City>
        Dim elementCity As XmlElement = document.CreateElement("City")
        Dim textCity As XmlText = document.CreateTextNode(arrName(10))
        elementCity.AppendChild(textCity)
        elementAddress.AppendChild(elementCity)

        ' Add <Address> as child element to <Person>
        elementPerson.AppendChild(elementAddress)

        ' Add root element <Person> to document
        document.AppendChild(elementPerson)

        document.Save(fileName)

    End Sub

End Class

Bob
0
 
Bob LearnedCommented:
Another way that you can achieve this is to have a serializable class that holds the values.

Bob
0
 
YRKSAuthor Commented:
I have never used an serialzale object before doesn't it mean I have to create a property for each eleent ad store it in a serializable class ad the call it.
0
 
Bob LearnedCommented:
Right, you would create something like this:

Imports System.IO
Imports System.Xml.Serialization

    <Serializable()> _
    Public Class Person

        <Serializable()> _
        Public Class Name
            Public FirstName As String
            Public MiddleName As String
            Public LastName As String
        End Class

        <Serializable()> _
        Public Class Address
            Public Street1 As String
            Public Street2 As String
            Public City As String
        End Class

        Public Sub Serialize(ByVal fileName As String)
            Dim serializer As New XmlSerializer(GetType(Person))
            Using stream As New FileStream(fileName, FileMode.Create)
                serializer.Serialize(stream, Me)
            End Using
        End Sub

    End Class

End Class

Bob
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now