Quantcast
Channel: XML, System.Xml, MSXML and XmlLite forum
Viewing all articles
Browse latest Browse all 935

Import string of XML into XML Document

$
0
0

I have a template xml string that I read from a db, I have then to insert xml strings from many transaction records into that document.  Here's a very cut down template..

<GovTalkMessage xmlns="http://www.govtalk.gov.uk/CM/envelope"><Body><IRenvelope xmlns="http://www.govtalk.gov.uk/taxation/PAYE/RTI/FullPaymentSubmission/13-14/2"><FullPaymentSubmission /></IRenvelope></Body></GovTalkMessage>

The transactional xml text needs to be added to the FullPaymentSubmission element..  Here's an example segment..

<Employee><EmployeeDetails><NINO>AA123456A</NINO><Name><Ttl>Mr</Ttl><Fore>Anthony</Fore><Sur>Brewer</Sur></Name></EmployeeDetails></Employee>

So the document should look like this.. 

<GovTalkMessage xmlns="http://www.govtalk.gov.uk/CM/envelope"><Body><IRenvelope xmlns="http://www.govtalk.gov.uk/taxation/PAYE/RTI/FullPaymentSubmission/13-14/2"><FullPaymentSubmission><Employee><EmployeeDetails><NINO>AA123456A</NINO><Name><Ttl>Mr</Ttl><Fore>Anthony</Fore><Sur>Brewer</Sur></Name></EmployeeDetails></Employee></FullPaymentSubmission></IRenvelope></Body></GovTalkMessage>

So, I tried the following code..

        Dim xBody, xTemp As XmlDocument
        Dim xEmp As XmlNode
        Dim xNode As XmlNode
        Dim strBody As String = "<GovTalkMessage xmlns=""http://www.govtalk.gov.uk/CM/envelope""><Body><IRenvelope xmlns=""http://www.govtalk.gov.uk/taxation/PAYE/RTI/FullPaymentSubmission/13-14/2""><FullPaymentSubmission /></IRenvelope></Body></GovTalkMessage>"
        Dim strEmp As String = "<Employee><EmployeeDetails><NINO>AA123456A</NINO><Name><Ttl>Mr</Ttl><Fore>Anthony</Fore><Sur>Brewer</Sur></Name></EmployeeDetails></Employee>"
        Dim GovTalkMessageNS As String
        Dim IRenvelopeNS As String

        xBody = New XmlDocument
        xBody.LoadXml(strBody)

        xTemp = New XmlDocument
        xTemp.LoadXml(strEmp)
        xEmp = xTemp.FirstChild

        ' find the namespace of the GovTalkMessage element (have to skip any number of comments)
        For Each xNode In xBody.ChildNodes
            If xNode.Name = "GovTalkMessage" Then
                GovTalkMessageNS = xNode.Attributes("xmlns").Value
                Exit For
            End If
        Next
        If GovTalkMessageNS = String.Empty Then
            Throw New Exception("Badly formed XML")
        End If

        ' Create a namespace manager for the namespace
        Dim nsmgr As New XmlNamespaceManager(xBody.NameTable)
        nsmgr.AddNamespace("gt", GovTalkMessageNS)
        IRenvelopeNS = xBody.SelectSingleNode("//gt:Body", nsmgr).FirstChild.Attributes("xmlns").Value
        nsmgr.AddNamespace("ir", IRenvelopeNS)

        xNode = xBody.SelectSingleNode("//ir:FullPaymentSubmission", nsmgr)

        xNode.AppendChild(xNode.OwnerDocument.ImportNode(xEmp, True))

Which does add the <employee> node to the correct element, but, it adds a default namespace attribute..

<IRenvelope xmlns="http://www.govtalk.gov.uk/taxation/PAYE/RTI/FullPaymentSubmission/13-14/2"><FullPaymentSubmission><Employee xmlns=""><EmployeeDetails>
How do I get rid of that extra attribute ?



Viewing all articles
Browse latest Browse all 935

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>