Search code examples
c#xmldocument

How do I correctly add an XML Namespace to my XMLDocument?


Currently my XmlDocument is not rendering the namespace tag in my output. I'm new to XmlDocument and I'm copying functionality from a older project in another language.

My output almost looks right, except the schema location is missing the namespace - as is every other instance of me trying to add it. My header and a random value tag example are below.

My literal output (removes the 'xsi:' I add in code):

    <ClinicalDocument
        xmlns="urn:hl7-org:v3"
        xmlns:mif="urn:hl7-org:v3/mif"
        xmlns:voc="urn:hl7-org:v3/voc"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" schemaLocation="urn:hl7-org:v3 CDA.xsd"> 
... 
<value type="CE" codeSystem="2.16.840.1.113883.6.96" code="55561003" displayName="Active"/>

My expected/required output (has 'xsi:' correctly applied)

<ClinicalDocument
    xmlns="urn:hl7-org:v3"
    xmlns:mif="urn:hl7-org:v3/mif"
    xmlns:voc="urn:hl7-org:v3/voc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:hl7-org:v3 CDA.xsd">
... 
<value xsi:type="CE" codeSystem="2.16.840.1.113883.6.96" code="55561003" displayName="Active"/>

My code:

    XmlDocument doc = new XmlDocument();
    XmlNode docNode = doc.CreateXmlDeclaration("1.0", null, null);
    doc.AppendChild(docNode);

    var node = doc.CreateElement("ClinicalDocument");
    XmlAttribute attribute;
    XmlElement element;

    attribute = doc.CreateAttribute("xmlns:xsi");
    attribute.Value = "http://www.w3.org/2001/XMLSchema-instance";
    node.Attributes.Append(attribute);

    attribute = doc.CreateAttribute("xsi:schemaLocation");
    attribute.Value = "urn:hl7-org:v3 CDA.xsd";
    node.Attributes.Append(attribute);

and later the value tag

    element5 = doc.CreateElement("value");
    element5.AddAttribute("xsi:type", "CD", doc);
    element5.AddAttribute("displayName", mytext, doc);

EDIT
As Youngjae pointed out below I needed to define the namespace separately by using the overloaded CreateAttribute method like so:

XmlAttribute typeAttr = doc.CreateAttribute("xsi", "type", xsiUri);

Thanks.


Solution

  • I tested below code:

    // Commonly used namespace
    string xsiUri = "http://www.w3.org/2001/XMLSchema-instance";
    
    // Same as your code to create root element
    XmlDocument doc = new XmlDocument();
    XmlNode docNode = doc.CreateXmlDeclaration("1.0", null, null);
    doc.AppendChild(docNode);
    
    var node = doc.CreateElement("ClinicalDocument");
    XmlAttribute attribute;
    XmlElement element;
    
    attribute = doc.CreateAttribute("xmlns:xsi");
    attribute.Value = xsiUri;
    node.Attributes.Append(attribute);
    
    attribute = doc.CreateAttribute("xsi:schemaLocation");
    attribute.Value = "urn:hl7-org:v3 CDA.xsd";
    node.Attributes.Append(attribute);
    
    // Child element: <value>
    element = doc.CreateElement("value");
    
    XmlAttribute typeAttr = doc.CreateAttribute("xsi", "type", xsiUri);
    typeAttr.Value = "CE";
    element.Attributes.Append(typeAttr);
    
    XmlAttribute displayNameAttr = doc.CreateAttribute("displayName");
    displayNameAttr.Value = "Active";
    element.Attributes.Append(displayNameAttr);
    
    node.AppendChild(element);
    

    And it gives below result

    <ClinicalDocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" schemaLocation="urn:hl7-org:v3 CDA.xsd">
        <value xsi:type="CE" displayName="Active" />
    </ClinicalDocument>