I am using the code below to change a namespace in an existing XML message in a BizTalk pipeline component. This works but how would I add a namespace alias to the document as well.
XNamespace toNs = "http://hl7.org/fhir/Encounters";
XElement doc = XElement.Parse(xmlIn);
doc.DescendantsAndSelf().Attributes().Where(a => a.IsNamespaceDeclaration).Remove();
var ele = doc.DescendantsAndSelf();
foreach (var el in ele)
el.Name = toNs + el.Name.LocalName;
return new XDocument(doc);
You can simply add a declaration attribute to the root. Take this example:
If you run this code:
You'll get this result:
See this fiddle for a demo.
Now that we know the reason for this (duplicate MessageTypes), the correct BizTalk way to handle this is to deploy Custom Pipelines with configured XmlDisassembler components. Everyone should be doing this anyway.
Please refer to this TechNet Wiki Article which describes this exact scenario and how to resolve it: BizTalk: Improve Deployment and Tracking by Always Creating Custom Pipelines
If you absolutely must modify the content, the correct way in a Pipeline Component is to use the XmlTranslatorStream. This is instead of XmlDocument or XDocument.
From a BizTalk perspective, the marked Answer is not correct. Sorry. :(