I'm using XmlWriter
and I'm struggling to create the following XML tag.
<mzML xmlns="http://psi.hupo.org/ms/mzml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://psi.hupo.org/ms/mzml http://psidev.info/files/ms/mzML/xsd/mzML1.1.0_idx.xsd" version="1.1">
I have the following:
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
XmlWriter xmlWriter = XmlWriter.Create(xmlFilePath, settings);
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("mzML", "xmlns", @"http://psi.hupo.org/ms/mzml");
xmlWriter.WriteAttributeString("xsi", "xmlns", @"http://www.w3.org/2001/XMLSchema-instance");
xmlWriter.WriteAttributeString("schemaLocation", "xsi", @"http://psi.hupo.org/ms/mzml http://psidev.info/files/ms/mzML/xsd/mzML1.1.0.xsd");
xmlWriter.WriteAttributeString("version", "1.1");
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
xmlWriter.Close();
which results in the following:
<mzML:xmlns p1:xsi="http://www.w3.org/2001/XMLSchema-instance" p2:schemaLocation="http://psi.hupo.org/ms/mzml http://psidev.info/files/ms/mzML/xsd/mzML1.1.0.xsd" version="1.1.0" xmlns:p2="xsi" xmlns:p1="xmlns" xmlns:mzML="http://psi.hupo.org/ms/mzml">
The documentation is confusing me; I've tried lots of variations of the above code but can't seem to get anywhere close to my target XML tag.
Can anybody please help?
(P.S. I need to use XmlWriter
due to the size of the XML files I need to create.)
This seems quite confused. If we go through each line in turn:
xmlWriter.WriteStartElement("mzML", "xmlns", @"http://psi.hupo.org/ms/mzml");
This specifies your element has the prefix mzML
and the local name xmlns
and the namespace of http://...
. Your element has no prefix, and the local name should be mzML
.
xmlWriter.WriteAttributeString("xsi", "xmlns",
@"http://www.w3.org/2001/XMLSchema-instance");
This writes an attribute with the prefix xsi
and the namespace xmlns
. This is a namespace declaration: it has a prefix of xmlns
, a local name of xsi
and the namespace should be null
. I'd also note that the writing of namespace declaration attributes will be automatically handled by XmlWriter
- you'd generally only write them explicitly if you wanted to control which element they occur in and/or in what order.
xmlWriter.WriteAttributeString("schemaLocation", "xsi",
@"http://psi.hupo.org/ms/mzml http://psidev.info/files/ms/mzML/xsd/mzML1.1.0.xsd");
This writes an attribute with the name schemaLocation
(correct) and the namespace xsi
. This is not correct, the namespace is http://www.w3.org/2001/XMLSchema-instance
.
xmlWriter.WriteAttributeString("version", "1.1");
This is correct.
Putting all of these changes together:
xmlWriter.WriteStartElement("mzML", @"http://psi.hupo.org/ms/mzml");
// these two lines are optional - the namespace declarations are
// automatically inserted as the *last* attributes when omitted
xmlWriter.WriteAttributeString("xmlns", null, null, "http://psi.hupo.org/ms/mzml");
xmlWriter.WriteAttributeString("xmlns", "xsi", null,
"http://www.w3.org/2001/XMLSchema-instance");
xmlWriter.WriteAttributeString("xsi", "schemaLocation",
"http://www.w3.org/2001/XMLSchema-instance",
"http://psi.hupo.org/ms/mzml http://psidev.info/files/ms/mzML/xsd/mzML1.1.0.xsd");
xmlWriter.WriteAttributeString("version", "1.1");
See this fiddle for a working demo. This leaves out the optional namespace declaration attributes to show they still get added.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With