Problem with namespace in XML document

G

Guest

Hello All,

I have the following XML document : (for ease of understanding i am just
posting a part of it)

Base XML
------------
<?xml version="1.0"?>
<ROOT>
<REPLY>DRAWLIST_INFO_REQUEST</REPLY>
<DECLARE_PREFIX xmlns:ENC="http://www.syswareinc.com/ENCOUNTR"
xmlns:SPECIMEN="http://www.syswareinc.com/SPECLOG">
<ACCESSION>
<ENC:NUMBER>58967</ENC:NUMBER>
<SPECIMEN>

<SPECIMEN:SPECIMENID><![CDATA[S0656363XX1]]></SPECIMEN:SPECIMENID>
<SPECIMEN:SPECSTATUS><![CDATA[Collection
Pending]]></SPECIMEN:SPECSTATUS>
</SPECIMEN>
<ENC:STATUS><![CDATA[Waiting for Specimen]]></ENC:STATUS>
</ACCESSION>
</DECLARE_PREFIX>
</ROOT>

This is the base xml. After some processing on the client side, the status
of the specimen is changed and for this we get a xml file from the server.
The contents of the xml file sent by the server is as follows :

Reply from server with updated specimen status - Reply XML
----------------------------------------------------------------------
<?xml version='1.0' ?>
<ROOT>
<DECLARE_PREFIX xmlns:ENC="http://www.syswareinc.com/ENCOUNTR"
xmlns:SPECIMEN="http://www.syswareinc.com/SPECLOG">
<ACCESSION>
<ENC:NUMBER>58967</ENC:NUMBER>
<SPECIMEN>
<SPECIMEN:SPECIMENID><![CDATA[S0656363XX1]]></SPECIMEN:SPECIMENID>
<SPECIMEN:SPECSTATUS><![CDATA[Collected]]></SPECIMEN:SPECSTATUS>
</SPECIMEN>
<ENC:STATUS><![CDATA[Waiting for Specimen]]></ENC:STATUS>
</ACCESSION>
</DECLARE_PREFIX>
</ROOT>

The reply xml is read and the ACCESSION tag and it's childnodes are taken
into a XmlNodelist. This XmlNodelist is passed as a parameter in the
UpdateXML method. In this method, the ENC:NUMBER is matched and the base xml
is updated with the contents of the xml file got from the server. Following
code shows how we do this :

Public Sub ReadCollectionsXml()
Dim LOC_XML_Document As XmlDocument
Dim LOC_RootElement As XmlElement
Dim LOC_XMLNode_Accession As XmlNodeList = Nothing

LOC_XML_Document = New XmlDocument
LOC_XML_Document.Load(Reply XML)
LOC_RootElement =
CType(LOC_XML_Document.DocumentElement.Item("DECLARE_PREFIX"), XmlElement)
LOC_XMLNode_Accession = LOC_RootElement.GetElementsByTagName("ACCESSION")

If Not IsNothing(LOC_XMLNode_Accession) Then
UpdateXML(LOC_XMLNode_Accession)
End If
End Sub

Public Sub UpdateXML(ByVal AccessionsToBeUpdated As XmlNodeList)

Dim LOC_XML_Document As New XmlDocument
Dim LOC_RootElement As XmlElement
Dim LOC_XMLNodeList As XmlNodeList
Dim LOC_NodeItem As XmlNode
Dim LOC_EachItem As Integer = 0
Dim LOC_EachAccession As Integer = 0
Dim LOC_XMLNode_Accession As XmlNode

LOC_XML_Document.Load(Base XML)
'Referring to the Root node
LOC_RootElement = LOC_XML_Document.DocumentElement
'XML node list to store all available accession number
LOC_XMLNodeList = LOC_RootElement.GetElementsByTagName("ENC:NUMBER")

'To visit each item of node list
For LOC_EachItem = 0 To AccessionsToBeUpdated.Count - 1
'To store each node
LOC_NodeItem = LOC_XML_Document.CreateNode(XmlNodeType.Element,
"ACCESSION", String.Empty)
'To assign content
LOC_NodeItem.InnerXml = AccessionsToBeUpdated.Item(LOC_EachItem).InnerXml
For LOC_EachAccession = 0 To LOC_XMLNodeList.Count - 1
'To store each accession node from login XML
LOC_XMLNode_Accession = LOC_XMLNodeList.Item(LOC_EachAccession)
'To check for the accession number
If
LOC_XMLNode_Accession.InnerText.Equals(LOC_NodeItem.ChildNodes(0).InnerText)
Then
'Replacing the <ACCESSION> tag

LOC_XMLNode_Accession.ParentNode.ParentNode.ReplaceChild(LOC_NodeItem,
LOC_XMLNode_Accession.ParentNode)
'Exiting from the inner loop
Exit For
End If
Next
Next

'Disposing the node list
LOC_XMLNodeList = Nothing

'Saving the updated xml file
LOC_XML_Document.Save(E_XML_FILEPATH & GLO_DRAWLIST_XML_NAME)
End Sub

This process is working absolutely fine but the problem is that after the
ReplaceChild method is called, the namespaces also get inserted in the tags
as shown below :

Base xml after the updation process
------------------------------------------
<?xml version="1.0"?>
<ROOT>
<DECLARE_PREFIX xmlns:ENC="http://www.syswareinc.com/ENCOUNTR"
xmlns:SPECIMEN="http://www.syswareinc.com/SPECLOG">
<ACCESSION>
<ENC:NUMBER
xmlns:ENC="http://www.syswareinc.com/ENCOUNTR">58967</ENC:NUMBER>
<SPECIMEN>
<SPECIMEN:SPECIMENID
xmlns:SPECIMEN="http://www.syswareinc.com/SPECLOG"><![CDATA[S0656363XX1]]></SPECIMEN:SPECIMENID>
<SPECIMEN:SPECSTATUS
xmlns:SPECIMEN="http://www.syswareinc.com/SPECLOG"><![CDATA[Collected]]></SPECIMEN:SPECSTATUS>
</SPECIMEN>
<ENC:STATUS
xmlns:ENC="http://www.syswareinc.com/ENCOUNTR"><![CDATA[Waiting for
Specimen]]></ENC:STATUS>
</ACCESSION>
</DECLARE_PREFIX>
</ROOT>

Can anyone please guide me as i don't want the namespaces to be inserted as
attributes of the tags ? Looking forward to your response.


Regards,
Viral
 
G

Guest

I have solved this problem by using ImportNode method of XMLDocument class.


Viral said:
Hello All,

I have the following XML document : (for ease of understanding i am just
posting a part of it)

Base XML
------------
<?xml version="1.0"?>
<ROOT>
<REPLY>DRAWLIST_INFO_REQUEST</REPLY>
<DECLARE_PREFIX xmlns:ENC="http://www.syswareinc.com/ENCOUNTR"
xmlns:SPECIMEN="http://www.syswareinc.com/SPECLOG">
<ACCESSION>
<ENC:NUMBER>58967</ENC:NUMBER>
<SPECIMEN>

<SPECIMEN:SPECIMENID><![CDATA[S0656363XX1]]></SPECIMEN:SPECIMENID>
<SPECIMEN:SPECSTATUS><![CDATA[Collection
Pending]]></SPECIMEN:SPECSTATUS>
</SPECIMEN>
<ENC:STATUS><![CDATA[Waiting for Specimen]]></ENC:STATUS>
</ACCESSION>
</DECLARE_PREFIX>
</ROOT>

This is the base xml. After some processing on the client side, the status
of the specimen is changed and for this we get a xml file from the server.
The contents of the xml file sent by the server is as follows :

Reply from server with updated specimen status - Reply XML
----------------------------------------------------------------------
<?xml version='1.0' ?>
<ROOT>
<DECLARE_PREFIX xmlns:ENC="http://www.syswareinc.com/ENCOUNTR"
xmlns:SPECIMEN="http://www.syswareinc.com/SPECLOG">
<ACCESSION>
<ENC:NUMBER>58967</ENC:NUMBER>
<SPECIMEN>
<SPECIMEN:SPECIMENID><![CDATA[S0656363XX1]]></SPECIMEN:SPECIMENID>
<SPECIMEN:SPECSTATUS><![CDATA[Collected]]></SPECIMEN:SPECSTATUS>
</SPECIMEN>
<ENC:STATUS><![CDATA[Waiting for Specimen]]></ENC:STATUS>
</ACCESSION>
</DECLARE_PREFIX>
</ROOT>

The reply xml is read and the ACCESSION tag and it's childnodes are taken
into a XmlNodelist. This XmlNodelist is passed as a parameter in the
UpdateXML method. In this method, the ENC:NUMBER is matched and the base xml
is updated with the contents of the xml file got from the server. Following
code shows how we do this :

Public Sub ReadCollectionsXml()
Dim LOC_XML_Document As XmlDocument
Dim LOC_RootElement As XmlElement
Dim LOC_XMLNode_Accession As XmlNodeList = Nothing

LOC_XML_Document = New XmlDocument
LOC_XML_Document.Load(Reply XML)
LOC_RootElement =
CType(LOC_XML_Document.DocumentElement.Item("DECLARE_PREFIX"), XmlElement)
LOC_XMLNode_Accession = LOC_RootElement.GetElementsByTagName("ACCESSION")

If Not IsNothing(LOC_XMLNode_Accession) Then
UpdateXML(LOC_XMLNode_Accession)
End If
End Sub

Public Sub UpdateXML(ByVal AccessionsToBeUpdated As XmlNodeList)

Dim LOC_XML_Document As New XmlDocument
Dim LOC_RootElement As XmlElement
Dim LOC_XMLNodeList As XmlNodeList
Dim LOC_NodeItem As XmlNode
Dim LOC_EachItem As Integer = 0
Dim LOC_EachAccession As Integer = 0
Dim LOC_XMLNode_Accession As XmlNode

LOC_XML_Document.Load(Base XML)
'Referring to the Root node
LOC_RootElement = LOC_XML_Document.DocumentElement
'XML node list to store all available accession number
LOC_XMLNodeList = LOC_RootElement.GetElementsByTagName("ENC:NUMBER")

'To visit each item of node list
For LOC_EachItem = 0 To AccessionsToBeUpdated.Count - 1
'To store each node
LOC_NodeItem = LOC_XML_Document.CreateNode(XmlNodeType.Element,
"ACCESSION", String.Empty)
'To assign content
LOC_NodeItem.InnerXml = AccessionsToBeUpdated.Item(LOC_EachItem).InnerXml
For LOC_EachAccession = 0 To LOC_XMLNodeList.Count - 1
'To store each accession node from login XML
LOC_XMLNode_Accession = LOC_XMLNodeList.Item(LOC_EachAccession)
'To check for the accession number
If
LOC_XMLNode_Accession.InnerText.Equals(LOC_NodeItem.ChildNodes(0).InnerText)
Then
'Replacing the <ACCESSION> tag

LOC_XMLNode_Accession.ParentNode.ParentNode.ReplaceChild(LOC_NodeItem,
LOC_XMLNode_Accession.ParentNode)
'Exiting from the inner loop
Exit For
End If
Next
Next

'Disposing the node list
LOC_XMLNodeList = Nothing

'Saving the updated xml file
LOC_XML_Document.Save(E_XML_FILEPATH & GLO_DRAWLIST_XML_NAME)
End Sub

This process is working absolutely fine but the problem is that after the
ReplaceChild method is called, the namespaces also get inserted in the tags
as shown below :

Base xml after the updation process
------------------------------------------
<?xml version="1.0"?>
<ROOT>
<DECLARE_PREFIX xmlns:ENC="http://www.syswareinc.com/ENCOUNTR"
xmlns:SPECIMEN="http://www.syswareinc.com/SPECLOG">
<ACCESSION>
<ENC:NUMBER
xmlns:ENC="http://www.syswareinc.com/ENCOUNTR">58967</ENC:NUMBER>
<SPECIMEN>
<SPECIMEN:SPECIMENID
xmlns:SPECIMEN="http://www.syswareinc.com/SPECLOG"><![CDATA[S0656363XX1]]></SPECIMEN:SPECIMENID>
<SPECIMEN:SPECSTATUS
xmlns:SPECIMEN="http://www.syswareinc.com/SPECLOG"><![CDATA[Collected]]></SPECIMEN:SPECSTATUS>
</SPECIMEN>
<ENC:STATUS
xmlns:ENC="http://www.syswareinc.com/ENCOUNTR"><![CDATA[Waiting for
Specimen]]></ENC:STATUS>
</ACCESSION>
</DECLARE_PREFIX>
</ROOT>

Can anyone please guide me as i don't want the namespaces to be inserted as
attributes of the tags ? Looking forward to your response.


Regards,
Viral
 
Top