如何在 XmlDocument 中添加多个不同的前缀属性

How to add multiple different prefixed attributes in XmlDocument

我一直在使用 C# 中的 XMLDocument 编写 xml。虽然向根元素前缀添加具有不同前缀的多个属性不会出现在输出 xml 文件中。

// desired output xml
<rqst xmlns="http://example.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" SampleVersion="5-1" xsi:schemaLocation="xyz.....">
<rqst>

XmlElement nodeDeclaration =objXMLDocument.CreateElement("rqst");
.....
// fourth attribute code
var objAttribute = objXMLDocument.CreateAttribute("xsi:schemaLocation");
objAttribute.InnerText ="xyz.....";
nodeDeclaration.Attributes.Append(objAttribute);

前三个属性写对了。第四个属性缺少前缀,在本例中为 "xsi"。

我怎样才能完成它?关于如何更正第四个属性的任何建议。

我自己得到了答案。

var fourthAttribute = objXMLDocument.CreateAttribute("xsi",   "schemaLocation", "http://www.w3.org/2001/XMLSchema-instance"); 
fourthAttribute.InnerText = "xyz.....";
nodeDeclaration.Attributes.Append(objAttribute);

这是应该的