Sending an XML Node to a Function for Processing

D

Dave

I am trying to run a fucntion to add and format the final XML message.
I tried passing the NodeBody to the Function (like I would have done
with VB6) but a scope error.

What's the best way to do this?


Thanks


Main Code:
------------------
Dim objXML As New Xml.XmlDocument
Dim nodeBody As Xml.XmlNode
Dim nodeTest1 As Xml.XmlNode
Dim nodeTest2 As Xml.XmlNode

nodeBody = objXML.CreateNode(XmlNodeType.Element, "Body", "")
nodeTest1 = objXML.CreateElement("TestValue1")
nodeTest2 = objXML.CreateElement("TestValue2")

nodeTest1.InnerText = "ActualValue1"
nodeTest2.InnerText = "ActualValue2"

nodeBody.AppendChild(nodeTest1)
nodeBody.AppendChild(nodeTest2)

objXML.AppendChild(nodeBody)

Dim xmlDave As New Xml.XmlDocument
xmlDave = AddXMLHeader(nodebody, "Test.XML", "TestXMLMessage")



Class Code:
-----------------
Public Function AddXMLHeader(ByVal nodePassed As Xml.XmlNode, ByVal
strFileName As String, ByVal strNodeName As String) As Xml.XmlDocument
'This procedure will add a header and the main body of the XML
message.
'
Dim xmlTemp As New Xml.XmlDocument
Dim nodeHeader As Xml.XmlNode
Dim nodeTimeStamp As Xml.XmlNode
Dim nodeFileName As Xml.XmlNode
Dim nodeWrite As Xml.XmlNode
nodeHeader = xmlTemp.CreateNode(Xml.XmlNodeType.Element, "Header",
"")
nodeTimeStamp = xmlTemp.CreateNode(Xml.XmlNodeType.Element,
"TimeStamp", "")
nodeFileName = xmlTemp.CreateNode(Xml.XmlNodeType.Element,
"FileName", "")
nodeWrite = xmlTemp.CreateNode(Xml.XmlNodeType.Element,
strNodeName, "")

nodeTimeStamp.InnerText = Now
nodeFileName.InnerText = strFileName
nodeHeader.AppendChild(nodeTimeStamp)
nodeHeader.AppendChild(nodeFileName)

nodeWrite.AppendChild(nodeHeader)
nodeWrite.AppendChild(nodePassed) <<<<<<<<<<<<<<<<<<<

xmlTemp.AppendChild(nodeWrite)
Return xmlTemp
End Function
 
S

Stephany Young

To clarify, the final XML should be structured likethis?:

<Header>
<TimeStamp />
<FileName />
<TestXMLMessage>
<Body>
<TestValue1 />
<TestValue2 />
</Body>

</TestXMLMessage>
</Header>

If, not then draw us a picture please.
 
M

Martin Honnen

Dave said:
I am trying to run a fucntion to add and format the final XML message.

Use only on XmlDocument instance to create all the nodes. Or, if you
need more than one XmlDocument instance then use ImportNode to import
nodes created by one document into a second document e.g.
nodeWrite.AppendChild(nodePassed) <<<<<<<<<<<<<<<<<<<

nodeWrite.AppendChild(_
nodeWrite.OwnerDocument.ImportNode(nodePassed, True))

You need to use ImportNode any time you want to insert/append a node
created by one document to a node created by a second document.
 
D

Dave

First, thanks for helping.

The variable nodePassed is declared as xml.xmlnode. When I look at
the details, it has the XML Text that I am looking form.

When I perform the appendchild, I get an cannot import nodes of type
'document'.

Thanks,

Dave
 
D

Dave

Ignore my last post. Your node.write.appendchild(node..... ) worked
like a charm.

Thanks for the help!

Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top