Home All Groups Group Topic Archive Search About

Sending an XML Node to a Function for Processing

Author
16 Dec 2006 5:40 AM
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

Author
16 Dec 2006 6:04 AM
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.


Show quoteHide quote
>    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) <<<<<<<<<<<<<<<<<<<


"Dave" <D***@Canada.com> wrote in message
news:tc17o25c08429k152pe1rmsn8cnd3u6dlq@4ax.com...
>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
Author
16 Dec 2006 3:11 PM
Martin Honnen
Dave wrote:
> 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.




--

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/
Author
18 Dec 2006 11:36 AM
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

On Sat, 16 Dec 2006 16:11:57 +0100, Martin Honnen <mahotr***@yahoo.de>
wrote:

Show quoteHide quote
>Dave wrote:
>> 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.
Author
18 Dec 2006 12:01 PM
Dave
Ignore my last post.  Your node.write.appendchild(node..... ) worked
like a charm.

Thanks for the help!

Dave

On Sat, 16 Dec 2006 16:11:57 +0100, Martin Honnen <mahotr***@yahoo.de>
wrote:

Show quoteHide quote
>Dave wrote:
>> 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.