目前,我的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);
谢谢。
我测试了以下代码:
// 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>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句