2016-06-21 14 views
0

Momentan rendert mein XmlDocument das Namespace-Tag nicht in meiner Ausgabe. Ich bin neu in XmlDocument und ich kopiere Funktionalität von einem älteren Projekt in einer anderen Sprache.Wie füge ich korrekt einen XML-Namespace zu meinem XMLDocument hinzu?

Meine Ausgabe sieht fast richtig aus, außer dass der Schema-Speicherort den Namespace fehlt - wie jede andere Instanz von mir versucht, es hinzuzufügen. Mein Header und ein zufälliges Wert-Tag Beispiel sind unten.

Mein wörtlicher Ausgang (entfernt die 'xsi:' Ich in Code hinzufügen):

<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"/> 

Meine erwartet/erforderliche Ausgabe (hat 'xsi:' korrekt angewandt)

<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"/> 

Meinen 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); 

und später der Wert-Tag

EDIT
Wie Youngjae wies darauf hin, unter ich unter Verwendung der überladenen Create Methode, wie so den Namensraum separat definieren benötigt:

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

Dank.

Antwort

1

I unten Code getestet:

// 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); 

Und es gibt unter Ergebnis

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

, sie habe - ich die überladene Methode Create verwenden musste, wie Sie darauf hingewiesen. Vielen Dank. –