Linq to xml xmlns=";“什么?”必须在父节点中

Linq to xml xmlns=";“什么?”必须在父节点中,linq-to-xml,root,xml-namespaces,xelement,xattribute,Linq To Xml,Root,Xml Namespaces,Xelement,Xattribute,我有个问题。不能这样命名XAttribute:XAttribute(“xmlns”,nss.NamespaceName) XNamespace ns=“urn:hl7 org:v3”; XNamespace nsVoc=“urn:hl7组织:v3/voc” 。 . 我不关闭这个家长在这里点头。。我有2000行Between end nod 消息是:不能在同一开始元素标记中将前缀“”从“”重新定义为“urn:hl7 org:v3”。而不是 new XElement("ClinicalDocume

我有个问题。不能这样命名XAttribute:XAttribute(“xmlns”,nss.NamespaceName)

XNamespace ns=“urn:hl7 org:v3”; XNamespace nsVoc=“urn:hl7组织:v3/voc”

。 .

我不关闭这个家长在这里点头。。我有2000行Between end nod

消息是:不能在同一开始元素标记中将前缀“”从“”重新定义为“urn:hl7 org:v3”。

而不是

new XElement("ClinicalDocument",
              new XAttribute(XNamespace.Xmlns + "xsi", xsiNs.NamespaceName),
              new XAttribute("xmlns", nss.NamespaceName),
你需要

XNamespace df = nss.NamespaceName;
new XElement(df + "ClinicalDocument", 
  new XAttribute(XNamespace.Xmlns + "xsi", xsiNs.NamespaceName),
              new XAttribute(XNamespace.Xmlns + "voc", nsVoc.NamespaceName),
              new XAttribute(xsiNs + "schemaLocation", ns.NamespaceName + "../Schemas/cda/Schemas/CDA.xsd"), 
 new XElement(df + "typeId",
因此,您需要确保为该默认名称空间创建一个XNamespace,然后您的代码需要确保
new-XElement()
调用使用例如
new-XElement(df+“foo”)
在正确的名称空间中创建元素

XNamespace df = nss.NamespaceName;
new XElement(df + "ClinicalDocument", 
  new XAttribute(XNamespace.Xmlns + "xsi", xsiNs.NamespaceName),
              new XAttribute(XNamespace.Xmlns + "voc", nsVoc.NamespaceName),
              new XAttribute(xsiNs + "schemaLocation", ns.NamespaceName + "../Schemas/cda/Schemas/CDA.xsd"), 
 new XElement(df + "typeId",