【发布时间】:2021-01-27 10:30:49
【问题描述】:
下面是我想要实现的格式。
<?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">
非常感谢您的帮助。
【问题讨论】:
-
使用
element.SetAttribute("noNamespaceSchemaLocation", "http://www.w3.org/2001/XMLSchema-instance", "eExact-Schema.xsd");,如this answer 到How to create XmlElement attributes with prefix? 所示。演示:dotnetfiddle.net/jNaqcg
标签: c# xml xml-namespaces