如何在创建XML文件时添加命名空间?

Ste*_*ven 10 c# xml

我必须在C#中创建一个XML文档.

根元素必须如下所示:

<valuation-request 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:noNamespaceSchemaLocation="valuations.xsd">
Run Code Online (Sandbox Code Playgroud)

我正在使用以下内容

XmlElement root = X.CreateElement("valuation-request");
root.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
root.SetAttribute("xsi:noNamespaceSchemaLocation", "valuations.xsd");
Run Code Online (Sandbox Code Playgroud)

然而这产生了

<valuation-request 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     noNamespaceSchemaLocation="valuations.xsd"> //missing the xsi:
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

Mub*_*han 8

使用SetAttribute的重载,它也接受名称空间:

root.SetAttribute("noNamespaceSchemaLocation", 
    "http://www.w3.org/2001/XMLSchema-instance", 
    "valuations.xsd"
); 
Run Code Online (Sandbox Code Playgroud)