Solved

Generating xmlDocument with VB 2005

Posted on 2006-11-01
8
320 Views
Last Modified: 2008-02-01
Hello,
 
I am starting a project that involves creating an xmlDocument and the following code creates the file, but the file is empty.  

Please let me know what I'm missing.

Thanks,
JMO9966


Imports System.Xml
Imports System.IO




Dim xmlRequestDoc As New XmlDocument()
        Dim itemID As String
        itemID = "AL01RD1.4"
        'Dim JBXML As String
        'JBXML = "<JBXML>"

        ' Add a price element.
        Dim newElem As XmlElement = xmlRequestDoc.CreateElement("JBXML")

        'Dim newElem2 As XmlElement = xmlRequestDoc.CreateElement(strJBXML)

        Dim newElem3 As XmlElement = xmlRequestDoc.CreateElement("MaterialAddRq")
        Dim newElem4 As XmlElement = xmlRequestDoc.CreateElement("MaterialAdd")

        Dim newElem5 As XmlElement = xmlRequestDoc.CreateElement("ID")
        newElem5.InnerText = itemID
        'xmlRequestDoc.DocumentElement.AppendChild(newElem5)

        ' Save the document to a file and auto-indent the output.
        Dim writer As XmlTextWriter = New XmlTextWriter("VBNET.xml", Nothing)
        writer.Formatting = Formatting.Indented
        xmlRequestDoc.Save(writer)
0
Comment
Question by:JMO9966
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 

Author Comment

by:JMO9966
ID: 17855025
I plan on building this document with the format below by stepping through my code and gathering each data element.  Thanks

<JBXML>
<JBXMLRequest Session="3637820800984FF2BDC495C5A38F34D3">
<JobAddRq>
<JobAdd>
<ID></ID>
<CustomerPO>8989</CustomerPO>
<Status>Active</Status>
<OrderDate>2006-08-10</OrderDate>
<OrderQty>111</OrderQty>
</JobAdd>
<SetUpCustomerInfo>
<CustomerRef ID="CATINC"></CustomerRef>
<OverrideCreditLimit>0</OverrideCreditLimit>
</SetUpCustomerInfo
</JobAddRq>
</JBXMLRequest>
</JBXML>
0
 
LVL 5

Expert Comment

by:DEEPESH
ID: 17855640
Your writer and elemnts are not assosiated , pease try this in following manner

Dim myxmlwriter As New  System.Xml.XmlTextWriter("c:\xmlfile.xml",Nothing)
myxmlwriter.WriteStartDocument()
myxmlwriter.WriteStartElement("JobAdd")
myxmlwriter.WriteElementString("CustomerPO", "8989")
myxmlwriter.WriteEndElement()
myxmlwriter.WriteEndDocument()
myxmlwriter.Flush()
myxmlwriter.Close()

This is not full code but try to write in this manner don't forgot to use flush an close

0
 
LVL 11

Expert Comment

by:melmers
ID: 17856086
Hi,

change your code like this:
Dim xmlRequestDoc As New XmlDocument()
        Dim itemID As String
        itemID = "AL01RD1.4"
        'Dim JBXML As String
        'JBXML = "<JBXML>"

        ' Add a price element.
        Dim newElem As XmlElement = xmlRequestDoc.CreateElement("JBXML")
         xmlRequestDoc.AppendChild(newElem)
        'Dim newElem2 As XmlElement = xmlRequestDoc.CreateElement(strJBXML)
         xmlRequestDoc.AppendChild(newElem2)
        Dim newElem3 As XmlElement = xmlRequestDoc.CreateElement("MaterialAddRq")
         xmlRequestDoc.AppendChild(newElem3)
        Dim newElem4 As XmlElement = xmlRequestDoc.CreateElement("MaterialAdd")
         xmlRequestDoc.AppendChild(newElem4)
        Dim newElem5 As XmlElement = xmlRequestDoc.CreateElement("ID")
        newElem5.InnerText = itemID
                 xmlRequestDoc.AppendChild(newElem5)
        'xmlRequestDoc.DocumentElement.AppendChild(newElem5)

        ' Save the document to a file and auto-indent the output.
       xmlrequestdoc.Save("File Name")

I have only added the appendChild to all Elements and replaced the Writer with the XMLDocument
.Save function.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:JMO9966
ID: 17857628
Thanks guys,

I'm following the second example to build the xml in memory instead of writing to a file but I appreciate the response DEEPESH.

I get an error when doing .AppendChild(newElem2)

     "This document already has a 'DocumentElement' node.



Thanks,
JMO9966
0
 
LVL 11

Expert Comment

by:melmers
ID: 17857706
The error comes from a copy &  paste error.  I have overseen your comment
at the elem2 Line.


Dim newElem As XmlElement = xmlRequestDoc.CreateElement("JBXML")
         xmlRequestDoc.AppendChild(newElem)

        Dim newElem3 As XmlElement = xmlRequestDoc.CreateElement("MaterialAddRq")
         newElem.AppendChild(newElem3)
        Dim newElem4 As XmlElement = xmlRequestDoc.CreateElement("MaterialAdd")
         newElem.AppendChild(newElem4)
        Dim newElem5 As XmlElement = xmlRequestDoc.CreateElement("ID")
        newElem5.InnerText = itemID
                 newElem.AppendChild(newElem5)
       

        ' Save the document to a file and auto-indent the output.
       xmlrequestdoc.Save("File Name")
0
 

Author Comment

by:JMO9966
ID: 17858859
thanks!  I was trying to appendchild to the doc instead of the element, my typo.

I'm planning on doing everything as Elements since I couldn't figure out how to get the top one ("JBXML") as a node instead of an element.  The structure in my file looks good so it probably doesn't matter??

0
 
LVL 11

Accepted Solution

by:
melmers earned 250 total points
ID: 17859274
I think if the top Element is a Node or an Element it doesn't matter.
The syntax of the file is always the same if node or element.
0
 

Author Comment

by:JMO9966
ID: 17861723
Thanks, ya the format looks good!
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

739 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