2016-06-21 55 views
0

目前我的XmlDocument不在我的輸出中呈現名稱空間標記。我是XmlDocument的新手,我從另一種語言的舊項目複製功能。如何正確地將XML Namespace添加到我的XMLDocument中?

我的輸出幾乎看起來是正確的,除了架構位置缺少命名空間 - 就像我嘗試添加它的每個其他實例一樣。我的標題和一個隨機值標記示例如下。

我的文字輸出(刪除 '的xsi:' 我添加在代碼):

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

我的預期/所需的輸出(已 '的xsi:' 正確應用)

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

我的代碼:

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

和後來的價值標籤

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

EDIT
作爲Youngjae指出下面我需要通過使用重載CreateAttribute方法,像這樣分別定義名稱空間:

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

感謝。

回答

1

我測試下面的代碼:

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

,它給下面的結果

<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

這做到了 - 我有你指出了使用重載CreateAttribute方法。謝謝。 –

相關問題