Solved

Generating xmlDocument with VB 2005

Posted on 2006-11-01
8
314 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

820 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