如何使用 xmlwriter 将 noNamespaceSchemaLocation 添加到 xml 的根元素

How to add noNamespaceSchemaLocation to root element of xml using xmlwirter

我有一个 xml 的序列化对象,我想将 xsi:noNamespaceSchemaLocation 添加到根元素,我正在使用 xmlwriter。

我希望我的 xml 喜欢下面的例子。

<?xml version="1.0" encoding="utf-8"?>
 <root xsi:noNamespaceSchemaLocation="abc.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <child>false</child>
 </root>

WriteStartElement 对我不起作用,因为它会在写入 fileWriter 对象后在最终 xml 中创建另一个根元素。

fileWriter.WriteStartElement("root");
fileWriter.WriteAttributeString("xsi", "noNamespaceSchemaLocation", 空, "abc.xsd");

我扩展了 xsd.exe 为我的 xsd 生成的部分 class 并在我的 class

中添加了一个成员

[XmlAttribute(AttributeName="noNamespaceSchemaLocation",Namespace="http://www.w3.org/2001/XMLSchema-instance")]
public string noNamespaceSchemaLocation = "abc.xsd";

现在输出是-

<?xml version="1.0" encoding="utf-8"?>
<root p1:noNamespaceSchemaLocation="abc.xsd" xmlns:p1="http://www.w3.org/2001/XMLSchema-instance">
  <child>false</child>
</root>

我想用 xsi 代替 p1.I 发现可能是因为 XmlAttribute 的无效属性,但不知道是什么。

无法使用 XMLDocuments,因为我想使用 xml 序列化。

我们将不胜感激。

由于命名空间为空,添加了 p1 前缀。当我从 xml 中删除默认命名空间时它起作用了,现在当我写部分 class.

时添加了 xsi 前缀