目前我的XmlDocument没有在输出中呈现命名空间标记。我是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);
修改
正如Youngjae在下面指出的那样,我需要使用重载的 CreateAttribute 方法单独定义命名空间,如下所示:
XmlAttribute typeAttr = doc.CreateAttribute("xsi", "type", xsiUri);
感谢。
答案 0 :(得分: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>