我正在尝试为 OpenClinica(一个使用 CDISC ODM XML 表示形式的临床试验平台)编写 XML 文件。我的问题是,当我尝试使用 XmlWriter 编写 XML 的第一个元素时,出现以下异常:
An exception of type 'System.Xml.XmlException' occurred in System.Xml.dll but
was not handled in user code
Additional information: The prefix '' cannot be redefined from '' to
'http://www.cdisc.org/ns/odm/v1.3' within the same start element tag.
Run Code Online (Sandbox Code Playgroud)
这是我想要在文件中包含的内容:
<ODM xmlns="http://www.cdisc.org/ns/odm/v1.3"
xmlns:OpenClinica="http://www.openclinica.org/ns/odm_ext_v130/v3.1"
xmlns:OpenClinicaRules="http://www.openclinica.org/ns/rules/v3.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
FileOID="testD20161121140900+0000"
Description="test"
CreationDateTime="2016-11-21T14:09:00+00:00"
FileType="Snapshot"
ODMVersion="1.3"
xsi:schemaLocation="http://www.cdisc.org/ns/odm/v1.3 OpenClinica-ODM1-3-0-OC2-0.xsd">
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
StringWriter swriter = new StringWriter();
XmlWriter writer = XmlWriter.Create(swriter);
writer.WriteStartElement("ODM");
writer.WriteAttributeString("xmlns", "http://www.cdisc.org/ns/odm/v1.3");
writer.WriteAttributeString("xmlns", "OpenClinica", null, "http://www.openclinica.org/ns/odm_ext_v130/v3.1");
writer.WriteAttributeString("xmlns","OpenClinicaRules",null, "http://www.openclinica.org/ns/rules/v3.1");
writer.WriteEndElement();
writer.Close();
return swriter.ToString();
Run Code Online (Sandbox Code Playgroud)
如果我尝试仅写入“xmlns:OpenClinica”和“xmlns:OpenClinicaRules”属性,则一切顺利,但当我尝试写入 xmlns 属性时会出现问题。
这里可能有什么问题?
请尝试以下操作:
writer.WriteStartElement("","ODM","http://www.cdisc.org/ns/odm/v1.3");
Run Code Online (Sandbox Code Playgroud)