XML Serializer C# Namespaces Duplicate Issue



I am using XmlSerializer in C# to generate an XML document based on a model. I need to generate the following XML root that contains a duplicate namespace using separate prefixes. Below is what the output should look like…



<ClinicalDocument xmlns:xsi="http://ift.tt/ra1lAU"
xsi:schemaLocation="urn:hl7-org:v3 CDA_SDTC.xsd"
xmlns="urn:hl7-org:v3"
xmlns:cda="urn:hl7-org:v3"
xmlns:sdtc="urn:hl7-org:sdtc">


However, when I Serialize this, the default entry is removed (which contains the duplicate namespace) and the root is prefixed.



<cda:ClinicalDocument xmlns:xsi="http://ift.tt/ra1lAU" xmlns:sdtc="urn:hl7-org:sdtc" xsi:schemaLocation="http://ift.tt/ra1lAU" xmlns:cda="urn:hl7-org:v3">


Here is my XmlSerializer code...



var writer = new XmlSerializer(clinicalDocument.GetType(),"urn:hl7-org:v3");

var myNamespace = new XmlSerializerNamespaces();

myNamespace.Add("sdtc", "urn:hl7-org:sdtc");
myNamespace.Add("xsi", "http://ift.tt/ra1lAU");
myNamespace.Add("cda", "urn:hl7-org:v3");


using (var file = new System.IO.StreamWriter(CCDUncOutputPath))
{
writer.Serialize(file, clinicalDocument, myNamespace);
file.Close();
};

writer = null;

GC.Collect();


Does anyone have a fix for this?


No comments:

Post a Comment