0

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");

Dinesh wakti
  • 121
  • 2
  • 8
  • 1
    [This](http://stackoverflow.com/questions/35417590/attribute-xmlnamespacedeclarations-is-ignored-during-xml-serialization) may possibly help to explain. – Ian Mar 22 '16 at 05:23

1 Answers1

0

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);
bkdev
  • 432
  • 4
  • 9