C# 需要使用XDocument创建XML文档的帮助吗
我在使用LINQ创建XML文档时遇到问题 我需要创建的XML文件有一个根元素“ClinicalDocument” XML需要如下所示C# 需要使用XDocument创建XML文档的帮助吗,c#,xml,linq,C#,Xml,Linq,我在使用LINQ创建XML文档时遇到问题 我需要创建的XML文件有一个根元素“ClinicalDocument” XML需要如下所示 <?xml version="1.0" encoding="utf-8"?> <ClinicalDocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:hl7-org:v3" xmlns:voc="urn:hl7-org:v3/voc" xmlns:sdt
<?xml version="1.0" encoding="utf-8"?>
<ClinicalDocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="urn:hl7-org:v3"
xmlns:voc="urn:hl7-org:v3/voc"
xmlns:sdtc="urn:hl7-org:sdtc">
<!-- Rest of the document here. -->
</ClinicalDocument>
尝试保存文件时出现异常
{“不能在同一开始元素标记中将前缀“”从“”重新定义为“urn:hl7 org:v3”。}
您创建
xsi
,然后创建,而不是像创建stdc
和voc
那样直接创建字符串,有什么特别的原因吗?没有,只是一个问题的复制粘贴。在文档中,我需要使用xsi名称空间
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XDeclaration dec = new XDeclaration("1.0", "UTF-8", null);
XDocument xDoc = new XDocument(dec,null);
XElement cDoc = new XElement("ClinicalDocumnet");
xDoc.Add(cDoc);
cDoc.Add(new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName));
cDoc.Add(new XAttribute("xmlns","urn:hl7-org:v3"));
cDoc.Add(new XAttribute(XNamespace.Xmlns + "stdc","urn:hl7-org:sdtc"));
cDoc.Add(new XAttribute(XNamespace.Xmlns + "voc","urn:hl7-org:v3/voc"));
xDoc.Save("C:\\test\\test.xml");
XNamespace ns = "urn:hl7-org:v3";
xDoc = new XDocument(dec, null);
cDoc = new XElement(ns + "ClinicalDocument");
xDoc.Add(cDoc);
cDoc.Add(new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"));
cDoc.Add(new XAttribute(XNamespace.Xmlns + "stdc","urn:hl7-org:sdtc"));
cDoc.Add(new XAttribute(XNamespace.Xmlns + "voc","urn:hl7-org:v3/voc"));