简体   繁体   English

将命名空间添加到xml根节点C#

[英]Add Namespace to an xml root node c#

I have a xml and I want to add 我有一个xml,我想添加

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../test/Schemas/test.xsd" xmlns:xsi =“ http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation =“ ../../../../ test / Schemas / test.xsd”

to xml root element programmatically in c# so that the xml looks like below. 在C#中以编程方式将xml根元素添加到xml中,以便xml如下所示。

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

<root>

what I tried 我尝试过的

doc.DocumentElement.SetAttribute("xmlns:xsi"," http://www.w3.org/2001/XMLSchema-instance "); doc.DocumentElement.SetAttribute(“ xmlns:xsi”,“ http://www.w3.org/2001/XMLSchema-instance ”); doc.DocumentElement.SetAttribute("xsi:noNamespaceSchemaLocation="../../../../test/Schemas/test.xsd"); doc.DocumentElement.SetAttribute(“ xsi:noNamespaceSchemaLocation =” ../../../../ test / Schemas / test.xsd“);

One possible solution is to create the xsi attribute using the CreateAttribute method and then adding this attribute to the root element like: 一种可能的解决方案是使用CreateAttribute方法创建xsi属性,然后将该属性添加到根元素中,例如:

XmlAttribute xsiAttr = doc.CreateAttribute("xsi", "noNamespaceSchemaLocation", "http://www.w3.org/2001/XMLSchema-instance");
xsiAttr.Value = "../../../../test/Schemas/test.xsd";
doc.DocumentElement.Attributes.Append(xsiAttr);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM