【发布时间】:2016-05-23 12:22:01
【问题描述】:
正如标题所说,我正在尝试将多个属性写入一个标签,但是,我一直遇到错误:
writer.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9");
writer.WriteAttributeString("xsi", "schemaLocation", null, "http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd");
writer.WriteAttributeString("xmlns", "xsi", "http://www.sitemaps.org/schemas/sitemap/0.9", "http://www.w3.org/2001/XMLSchema-instance");
是我正在使用的。
第一行给出了这个错误:
System.Xml.XmlException:前缀 '' 不能在同一起始元素标记内从 '' 重新定义为 'http://www.sitemaps.org/schemas/sitemap/0.9'。
如果你删除它,第三行会给出:
System.ArgumentException: 前缀“xmlns”保留给 XML 使用。
有人有什么想法吗?我看不出发生这种情况的原因。
【问题讨论】:
-
我认为有一种特殊的方法可以添加命名空间......据我所知,这就像使用一些
NamespaceManager之类的东西......谷歌它...... -
您应该将默认命名空间传递给 WriteStartElement 方法。见C# XML - Multiple Namespace Declaration with XML Writer。