Help with creating xml file using VB.NET

Hi,

From button click event of my application, how do I create a new xml with the following data elements using VB.NET?


<?xml version="1.0" standalone="yes"?>
<DocumentElement>
  <MyDataTable>
    <LinkAID>0</LinkAID>
    <ItemA></ItemA>
    <ItemB></ItemB>
   <ItemC></ItemC>
   </MyDataTable>
</DocumentElement>

Thanks,

Victor
vcharlesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
as XML is, to start with, nothing else than "plain text/string", with though some "formatting" structure to be parseable, the question is where does your data come from, and what is your actual problem?

* building the XML string
* saving the file
* fetching the data

please clarify
0
vcharlesAuthor Commented:
Hi,

I need to build the XML string in a new file and save the file. The goal is to hard code the data elements from my button click event and have the following xml file in my application's folder.

Newfile.xml:

<?xml version="1.0" standalone="yes"?>
<DocumentElement>
  <MyDataTable>
    <LinkAID>0</LinkAID>
    <ItemA></ItemA>
    <ItemB></ItemB>
   <ItemC></ItemC>
   </MyDataTable>
</DocumentElement>
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
again, what EXACTLY is the problem?
0
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

vcharlesAuthor Commented:
How do I write the code in VB.NET to create the XML file (Newfile.xml)?
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
some sample code:
Private Sub writeDebug(ByVal x As String)

    Dim FILE_NAME As String ="C:\newfile.xml"

    If System.IO.File.Exists(FILE_NAME) = False Then
        System.IO.File.Create(FILE_NAME).Dispose()
    End If

    Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)
    objWriter.WriteLine(x)
    objWriter.Close()
End Sub 

Open in new window


and you call that function with the xml string as argument
0
SAMIR BHOGAYTAFreelancer and IT ConsultantCommented:
hi.. Can you please use this example. It is very useful for creating xml file in vb.net

Imports System.Xml

Module XmlModule
    ''' <summary>
    ''' Employee type.
    ''' </summary>
    Class Employee
      Public Sub New(ByVal id As Integer, ByVal firstName As String, _
                   ByVal lastName As String, ByVal salary As Integer)
          ' Set fields.
          Me._id = id
          Me._firstName = firstName
          Me._lastName = lastName
          Me._salary = salary
      End Sub

      ' Storage of employee data.
      Public _firstName As String
      Public _id As Integer
      Public _lastName As String
      Public _salary As Integer
    End Class

    Sub Main()
      ' Create array of employees.
      Dim employees(2) As Employee
      employees(0) = New Employee(1, "Prakash", "Rangan", 70000)
      employees(1) = New Employee(5, "Norah", "Miller", 21000)
      employees(2) = New Employee(17, "Cecil", "Walker", 60000)

      ' Create XmlWriterSettings.
      Dim settings As XmlWriterSettings = New XmlWriterSettings()
      settings.Indent = True

      ' Create XmlWriter.
      Using writer As XmlWriter = XmlWriter.Create("C:\employees.xml", settings)
          ' Begin writing.
          writer.WriteStartDocument()
          writer.WriteStartElement("Employees") ' Root.

          ' Loop over employees in array.
          Dim employee As Employee
          For Each employee In employees
            writer.WriteStartElement("Employee")
            writer.WriteElementString("ID", employee._id.ToString)
            writer.WriteElementString("FirstName", employee._firstName)
            writer.WriteElementString("LastName", employee._lastName)
            writer.WriteElementString("Salary", employee._salary.ToString)
            writer.WriteEndElement()
          Next

          ' End document.
          writer.WriteEndElement()
          writer.WriteEndDocument()
      End Using
    End Sub
End Module

Output :
----------------
<?xml version="1.0" encoding="utf-8"?>
<Employees>
  <Employee>
    <ID>1</ID>
    <FirstName>Prakash</FirstName>
    <LastName>Rangan</LastName>
    <Salary>70000</Salary>
  </Employee>
  <Employee>
    <ID>5</ID>
    <FirstName>Norah</FirstName>
    <LastName>Miller</LastName>
    <Salary>21000</Salary>
  </Employee>
  <Employee>
    <ID>17</ID>
    <FirstName>Cecil</FirstName>
    <LastName>Walker</LastName>
    <Salary>60000</Salary>
  </Employee>
</Employees>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vcharlesAuthor Commented:
Thank You.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.