Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Generating xmlDocument with VB 2005

Posted on 2006-11-01
8
Medium Priority
?
331 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
Independent Software Vendors: 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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

722 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