Solved

Generating xmlDocument with VB 2005

Posted on 2006-11-01
8
310 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
  • 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
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 …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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