Antoine Thiry Antoine Thiry - 18 days ago 6
C# Question

Rewriting namespace with XmlWriter give me XmlException

I'm trying to write an XML file for OpenClinica, a clinical trial platform who uses CDISC ODM XML representation.
My problem is when I try to write the very first element of the XML with an XmlWriter, I have this exception :

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.


Here's what I want in my file :

<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">


And here's my code :

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();


If I try to write only the "xmlns:OpenClinica" and the "xmlns:OpenClinicaRules" attributes, it's going well but the problem occurs when I try to write the xmlns attribute.

What could be the problem here ?

Answer

Try the following:

writer.WriteStartElement("","ODM","http://www.cdisc.org/ns/odm/v1.3");