xml 的 noNamespaceSchemaLocation 中缺少 xsi

问题描述

以下是我想要实现的格式。

<?xml version="1.0" ?>
<eExact xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="eExact-Schema.xsd">

这是我到目前为止的代码,缺少 xsi

            var doc = new XmlDocument();
            XmlNode docNode = doc.CreateXmlDeclaration("1.0",null,null);
            doc.AppendChild(docNode);
            XmlNode rootNode = doc.CreateElement("eExact");
            doc.AppendChild(rootNode);

            XmlElement element = doc.DocumentElement;
            element.SetAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
            element.SetAttribute("noNamespaceSchemaLocation","eExact-Schema.xsd");

下面是我从这段代码中得到的 xml 结果:

<?xml version="1.0"?>
<eExact xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" noNamespaceSchemaLocation="eExact-Schema.xsd">

非常感谢您的帮助。

解决方法

您可以在调用 https://docs.microsoft.com/en-us/dotnet/api/system.xml.xmlelement.setattribute?view=net-5.0#System_Xml_XmlElement_SetAttribute_System_String_System_String_System_String_

中添加命名空间