如何正确地将 XML 命名空间添加到我的 XML 文档中?
How do I correctly add an XML Namespace to my XMLDocument?
目前我的 XmlDocument 没有在我的输出中呈现名称空间标记。我是 XmlDocument 的新手,我正在用另一种语言从旧项目复制功能。
我的输出看起来几乎是正确的,除了模式位置缺少命名空间——就像我尝试添加它的每个其他实例一样。下面是我的 header 和一个随机值标记示例。
我的文字输出(删除我在代码中添加的'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"/>
我的 expected/required 输出(已正确应用 '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>
目前我的 XmlDocument 没有在我的输出中呈现名称空间标记。我是 XmlDocument 的新手,我正在用另一种语言从旧项目复制功能。
我的输出看起来几乎是正确的,除了模式位置缺少命名空间——就像我尝试添加它的每个其他实例一样。下面是我的 header 和一个随机值标记示例。
我的文字输出(删除我在代码中添加的'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"/>
我的 expected/required 输出(已正确应用 '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>