Suppress Xmlns in InnerXML / Rename an XML node in DOM
-
Suppress Xmlns in InnerXML / Rename an XML node in DOM The Original: <?xml version="1.0" encoding="utf-8"?> <EntrySummaryDeclaration xmlns="http://www.gph.ie/schemas/ics/IE315/v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.gph.ie/schemas/ics/IE315/v1 C:/GPH/Schemas/IE315/v1/schema.xsd"> <Declaration> : : </Declaration> </EntrySummaryDeclaration> The Required new output <?xml version="1.0" encoding="utf-8"?> <EntrySummaryDeclarationAmendment xmlns="http://www.gph.ie/schemas/ics/IE313/v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.gph.ie/schemas/ics/IE313/v1 C:/GPH/Schemas/IE313/v1/schema.xsd"> <Declaration> : : </Declaration> </ EntrySummaryDeclarationAmendment > The Actual New Output <?xml version="1.0" encoding="utf-8"?> <EntrySummaryDeclarationAmendment xmlns="http://www.gph.ie/schemas/ics/IE313/v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" schemaLocation="http://www.gph.ie/schemas/ics/IE313/v1 http://wlhost2:38711/xmlreferencesite/customs/ics/IE313-EntrySummaryDeclarationAmendment/v1/schema.xsd C:/GPH/Schemas/IE313/v1/schema.xsd"> <Declaration xmlns="http://www.gph.ie/schemas/ics/IE313/v1"> </Declaration></EntrySummaryDeclarationAmendment> The approach I am using DOM, so I cant rename EntrySummaryDeclaration, therefore I create a new EntrySummaryDeclarationAmendment node, and I assign the inner xml of EntrySummaryDeclaration to the inner xml of EntrySummaryDeclarationAmendment before deleting EntrySummaryDeclaration. The problem: The innerXml insists on bringing its original xmlns with it. The requested solution: How do I suppress the xmlns in the innerxml – removing it as an attribute after the fact has no effect? Alternatively – how would I rename EntrySummaryDeclaration as EntrySummaryDeclarationAmendment easly? The offending Code XmlNode ^TmpNode; XmlNode ^IE315_Node; XmlNode ^IE313_Node; XmlAttribute ^att; String^ tmpstr; if (current_message == MessageType::IE315) { if (!namespace_found) try { IE315_Node = doc->SelectSingleNode("//EntrySummaryDeclaration"); } catch (Exception ^e) { MessageString = "Error finding EntrySummaryDeclaration: " + e->Message; MessageBox::Show(MessageString); } else try { IE315_Node = doc->SelectSingleNode("
-
Suppress Xmlns in InnerXML / Rename an XML node in DOM The Original: <?xml version="1.0" encoding="utf-8"?> <EntrySummaryDeclaration xmlns="http://www.gph.ie/schemas/ics/IE315/v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.gph.ie/schemas/ics/IE315/v1 C:/GPH/Schemas/IE315/v1/schema.xsd"> <Declaration> : : </Declaration> </EntrySummaryDeclaration> The Required new output <?xml version="1.0" encoding="utf-8"?> <EntrySummaryDeclarationAmendment xmlns="http://www.gph.ie/schemas/ics/IE313/v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.gph.ie/schemas/ics/IE313/v1 C:/GPH/Schemas/IE313/v1/schema.xsd"> <Declaration> : : </Declaration> </ EntrySummaryDeclarationAmendment > The Actual New Output <?xml version="1.0" encoding="utf-8"?> <EntrySummaryDeclarationAmendment xmlns="http://www.gph.ie/schemas/ics/IE313/v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" schemaLocation="http://www.gph.ie/schemas/ics/IE313/v1 http://wlhost2:38711/xmlreferencesite/customs/ics/IE313-EntrySummaryDeclarationAmendment/v1/schema.xsd C:/GPH/Schemas/IE313/v1/schema.xsd"> <Declaration xmlns="http://www.gph.ie/schemas/ics/IE313/v1"> </Declaration></EntrySummaryDeclarationAmendment> The approach I am using DOM, so I cant rename EntrySummaryDeclaration, therefore I create a new EntrySummaryDeclarationAmendment node, and I assign the inner xml of EntrySummaryDeclaration to the inner xml of EntrySummaryDeclarationAmendment before deleting EntrySummaryDeclaration. The problem: The innerXml insists on bringing its original xmlns with it. The requested solution: How do I suppress the xmlns in the innerxml – removing it as an attribute after the fact has no effect? Alternatively – how would I rename EntrySummaryDeclaration as EntrySummaryDeclarationAmendment easly? The offending Code XmlNode ^TmpNode; XmlNode ^IE315_Node; XmlNode ^IE313_Node; XmlAttribute ^att; String^ tmpstr; if (current_message == MessageType::IE315) { if (!namespace_found) try { IE315_Node = doc->SelectSingleNode("//EntrySummaryDeclaration"); } catch (Exception ^e) { MessageString = "Error finding EntrySummaryDeclaration: " + e->Message; MessageBox::Show(MessageString); } else try { IE315_Node = doc->SelectSingleNode("
... I avoided the need to do either by using OuterXML: XmlNode ^TmpNode; XmlNode ^IE315_Node; XmlNode ^IE313_Node; String^ tmpstr; switch (current_message) { case (MessageType::IE315): { if (!namespace_found) try { IE315_Node = doc->SelectSingleNode("//EntrySummaryDeclaration"); } catch (Exception ^e) { MessageString = "Error finding EntrySummaryDeclaration: " + e->Message; MessageBox::Show(MessageString); } else try { IE315_Node = doc->SelectSingleNode("//ie:EntrySummaryDeclaration",nsmgr); } catch (Exception ^e) { MessageString = "Error finding EntrySummaryDeclaration: " + e->Message; MessageBox::Show(MessageString); } tmpstr = IE315_Node->OuterXml->ToString(); tmpstr = tmpstr->Replace("EntrySummaryDeclaration", "EntrySummaryDeclarationAmendment"); tmpstr = tmpstr->Replace("IE315", "IE313"); doc->RemoveChild(IE315_Node); doc->LoadXml(tmpstr); current_message = MessageType::IE313; Convert->Text = "Con&vert to IE315"; ReferenceNumber->Text = ""; ReferenceNumber->Enabled = true; break; }
Ger