Link to home
Start Free TrialLog in
Avatar of YRKS
YRKS

asked on

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>
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

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
Avatar of YRKS
YRKS

ASKER

Yes I do have control over the XML.
YRKS
Ok, that answers #2, how about #1?

Bob
Avatar of YRKS

ASKER

yes I am asking how to add elements to an xml document from an array
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
Another way that you can achieve this is to have a serializable class that holds the values.

Bob
Avatar of YRKS

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial