Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 需要使用XDocument创建XML文档的帮助吗_C#_Xml_Linq - Fatal编程技术网

C# 需要使用XDocument创建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

我在使用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: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"));