2016-06-21 6 views
0

現在、私のXmlDocumentは出力に名前空間タグを表示していません。私はXmlDocumentが新しく、古いプロジェクトの機能を別の言語でコピーしています。XMLDocumentにXML名前空間を正しく追加するにはどうすればよいですか?

スキーマの場所に名前空間がないことを除いて、私の出力はほぼ正しいように見えます。私のヘッダーとランダムな値のタグの例は次のとおりです。

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

私のコード

私のリテラル出力は(:私はコードに追加 'XSI' を削除します) :

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
私はそうのような過負荷にcreateAttributeをメソッドを使用して、個別に名前空間を定義するために必要なの下Youngjaeが指摘したように:

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をメソッドを使用する必要がありました。ありがとう。 –

関連する問題