简体   繁体   English

C#XmlWriter命名空间问题

[英]C# XmlWriter namespace issues

I'm using XmlWriter and I'm struggling to create the following XML tag. 我正在使用XmlWriter ,正在努力创建以下XML标签。

<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. 我已经尝试了上述代码的许多变体,但似乎无法达到我的目标XML标签的任何地方。

Can anybody please help? 有人可以帮忙吗?

(PS I need to use XmlWriter due to the size of the XML files I need to create.) (由于我需要创建XML文件的大小,因此我需要使用XmlWriter 。)

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://... . 这指定您的元素具有前缀mzML和本地名称xmlns以及http://...的名称空间。 Your element has no prefix, and the local name should be mzML . 您的元素没有前缀, 本地名称应为mzML

xmlWriter.WriteAttributeString("xsi", "xmlns", 
    @"http://www.w3.org/2001/XMLSchema-instance");

This writes an attribute with the prefix xsi and the namespace xmlns . 这将写入带有前缀xsi和名称空间xmlns的属性。 This is a namespace declaration: it has a prefix of xmlns , a local name of xsi and the namespace should be null . 这是一个名称空间声明:它具有xmlns前缀,本地名称xsi并且名称空间应为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自动处理-通常,如果要控制它们以什么元素和/或以什么顺序发生,通常只应明确地编写它们。

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 . 这将写入名称为schemaLocation (正确)和名称空间xsi的属性。 This is not correct, the namespace is http://www.w3.org/2001/XMLSchema-instance . 这是不正确的,名称空间是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. 这省去了可选的名称空间声明属性,以显示它们仍然被添加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM