Solved

create XML file with multi level nodes

Posted on 2008-06-09
6
1,246 Views
Last Modified: 2013-11-18
I'm trying to create XML file similar to the one below:
+ <Header>
  <MessageType>Release</MessageType>
  <DateTime>2008-03-26 19:05:12</DateTime>
  <EventType>Created</EventType>
  </Header>
- <Body>
- <ReleaseTask>
- <Origine>
  <Name />
- <References>
  <Name />
  <Value>E-17</Value>
  </References>
  </Origine>
  <LoadType>EMPTY</LoadType>
  </ReleaseTask>
  </Body>
I know how to create a Header section but have issue with the multi level Body section. I'm using VB .NET in VS 2005 Xml.XmlDocument and Xml.XmlNode syntax but can not get that work. Can you provide a sample code to me. Thank you.
0
Comment
Question by:jurekm61
  • 3
  • 3
6 Comments
 
LVL 12

Expert Comment

by:Jeff Darling
ID: 21746773
Your XML is invalid.  XML documents must have a single root element.

http://www.w3schools.com/xml/xml_dtd.asp


<?xml version="1.0"?>

<myroot>

  <Header>

    <MessageType>Release</MessageType>

    <DateTime>2008-03-26 19:05:12</DateTime>

    <EventType>Created</EventType>

  </Header>

  <Body>

    <ReleaseTask>

      <Origine>

        <Name/>

        <References>

          <Name/>

          <Value>E-17</Value>

        </References>

     </Origine>

     <LoadType>EMPTY</LoadType>

    </ReleaseTask>

   </Body>

</myroot>

Open in new window

0
 

Author Comment

by:jurekm61
ID: 21746862
My full XML file includes <myroot> </myroot>  tags and it looks exactly the way you showed above. I did not include them in my example as I felt it was not relevant to my question.

I'm just asking how do I create node levels between <Body></Body> tags ?
0
 
LVL 12

Expert Comment

by:Jeff Darling
ID: 21747137
Here is one way to do it using xmlwriter.


Imports System.xml
 

Public Class Form1
 
 
 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 

        Dim settings As New XmlWriterSettings()

        settings.Indent = True

        settings.IndentChars = "  "

        settings.NewLineChars = vbCrLf
 

        Using writer As XmlWriter = XmlWriter.Create("c:\jeffd\vb\makexml\output.xml", settings)

            writer.WriteStartElement("myroot")
 

            writer.WriteStartElement("Header")

            writer.WriteElementString("MessageType", "Release")

            writer.WriteElementString("DateTime", "2008-03-26 19:05:12")

            writer.WriteElementString("EventType", "Created")

            writer.WriteEndElement()
 

            writer.WriteStartElement("Body")
 

            writer.WriteStartElement("ReleaseTask")
 

            writer.WriteStartElement("Origin")

            writer.WriteElementString("Name", "")
 

            writer.WriteStartElement("References")

            writer.WriteElementString("Name", "")

            writer.WriteElementString("Value", "E-17")

            writer.WriteEndElement()
 

            writer.WriteEndElement()

            writer.WriteElementString("LoadType", "EMPTY")
 
 
 
 

            writer.Flush()

        End Using
 

    End Sub

End Class

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:jurekm61
ID: 21747677
Can you provide an example how to achive the same by using Xml.XmlDocument and nodes. Thanks for your help. I want to understand that better by looking at different examples.
0
 
LVL 12

Accepted Solution

by:
Jeff Darling earned 125 total points
ID: 21753562
It is much harder to do that way, but here it is...
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
 

        Dim objDocument As New System.Xml.XmlDocument
 

        Dim objXmlNode As System.Xml.XmlNode
 

        Dim objXmlNodeNew As System.Xml.XmlNode

        Dim objXmlNodeNew2 As System.Xml.XmlNode

        Dim objXmlNodeNew3 As System.Xml.XmlNode

        Dim objXmlNodeNew4 As System.Xml.XmlNode

        Dim objXmlNodeNew5 As System.Xml.XmlNode
 

        objDocument.LoadXml("<myroot></myroot>")
 

        objXmlNodeNew = objDocument.CreateElement("Header")
 

        objXmlNode = objDocument.DocumentElement
 

        objXmlNodeNew2 = objDocument.CreateElement("MessageType")
 

        objXmlNodeNew2.InnerText = "Release"
 

        objXmlNodeNew.AppendChild(objXmlNodeNew2)
 

        objXmlNodeNew2 = objDocument.CreateElement("DateTime")
 

        objXmlNodeNew2.InnerText = "2008-03-26 19:05:12"
 

        objXmlNodeNew.AppendChild(objXmlNodeNew2)
 

        objXmlNodeNew2 = objDocument.CreateElement("EventType")
 

        objXmlNodeNew2.InnerText = "Created"
 

        objXmlNodeNew.AppendChild(objXmlNodeNew2)
 
 
 

        objXmlNode.AppendChild(objXmlNodeNew)
 

        objXmlNode = objDocument.DocumentElement
 

        'Body
 

        objXmlNodeNew = objDocument.CreateElement("Body")
 

        objXmlNode = objDocument.DocumentElement
 

        objXmlNode.AppendChild(objXmlNodeNew)
 
 

        'ReleaseTask
 

        objXmlNodeNew2 = objDocument.CreateElement("ReleaseTask")
 

        objXmlNodeNew.AppendChild(objXmlNodeNew2)
 

        'Origin
 

        objXmlNodeNew3 = objDocument.CreateElement("Origin")
 

        objXmlNodeNew2.AppendChild(objXmlNodeNew3)
 

        'Name
 

        objXmlNodeNew4 = objDocument.CreateElement("Name")
 

        objXmlNodeNew4.InnerText = ""
 

        objXmlNodeNew3.AppendChild(objXmlNodeNew4)
 

        'References

        objXmlNodeNew4 = objDocument.CreateElement("References")
 

        objXmlNodeNew3.AppendChild(objXmlNodeNew4)
 
 

        'Name

        objXmlNodeNew5 = objDocument.CreateElement("Name")
 

        objXmlNodeNew5.InnerText = ""
 

        objXmlNodeNew4.AppendChild(objXmlNodeNew5)
 

        'Value

        objXmlNodeNew5 = objDocument.CreateElement("Value")
 

        objXmlNodeNew5.InnerText = "E-17"
 

        objXmlNodeNew4.AppendChild(objXmlNodeNew5)
 
 

        'LoadType
 

        objXmlNodeNew3 = objDocument.CreateElement("LoadType")
 

        objXmlNodeNew3.InnerText = "EMPTY"
 

        objXmlNodeNew2.AppendChild(objXmlNodeNew3)
 
 

        TextBox1.Text = objDocument.InnerXml

Open in new window

0
 

Author Closing Comment

by:jurekm61
ID: 31465485
Thanks for your help. It was up-to-the-point solution I was looking for..
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Enable Intel-VT on a IBM Yoga 7 30
Fixed div within Bootstrap carousel item 11 54
Transform XML to Excel using XSL 1 23
problems with mysql ODBC Connector and vbscript 4 25
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now