C# XML - Multiple Namespace Declaration with XML Writer

≡放荡痞女 提交于 2019-12-01 18:05:36
writer.WriteStartElement("BOF"); // write element name BOF, no prefix, namespace ""
writer.WriteAttributeString("xmlns", null, null, "http://www.acme.com/BOF");  //Set namespace for no prefix to "http://www.acme.com/BOF".

The second line makes no sense, because you're assigning the default (no-prefix) namespace to something other than what it is, in the same place as it is that.

Replace those two lines with writer.WriteStartElement("BOF", "http://www.acme.com/BOF")

You should pass your default namespace to the WriteStartElement method.

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

Should be written as

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

In that case the prefix xsi is registered at XML name table. Later use of http://www.w3.org/2001/XMLSchema-instance for parameter ns at a method of XmlWriter will prepend the XML namespace prefix of xsi.

URI of XML namespace xsi is also available at .NET by constant System.Xml.Schema.XmlSchema.InstanceNamespace.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!