0
votes

I have a xml and I want to add

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.

<?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("xsi:noNamespaceSchemaLocation="../../../../test/Schemas/test.xsd");

1
This may possibly help to explain.Ian

1 Answers

0
votes

One possible solution is to create the xsi attribute using the CreateAttribute method and then adding this attribute to the root element like:

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