Solved

Generating xmlDocument with VB 2005

Posted on 2006-11-01
8
292 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
 

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 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

10 Experts available now in Live!

Get 1:1 Help Now