Avoid extra xmlns:xsi while adding an attribute to XML root

Posted by Rakesh kumar on Stack Overflow See other posts from Stack Overflow or by Rakesh kumar
Published on 2010-06-18T12:39:22Z Indexed on 2010/06/18 13:23 UTC
Read the original article Hit count: 833

Filed under:
|

I am creating an xml file using this code:

XmlDocument d = new XmlDocument();
XmlDeclaration xmlDeclaration = d.CreateXmlDeclaration("1.0", "utf-8", null);
d.InsertBefore(xmlDeclaration,d.DocumentElement);
XmlElement root = d.CreateElement("ITRETURN","ITR","http://incometaxindiaefiling.gov.in/ITR1");
XmlAttribute xsiNs = d.CreateAttribute("xsi:schemaLocation", "http://www.w3.org/2001/XMLSchema-instance");
xsiNs.Value = "http://incometaxindiaefiling.gov.in/main ITRMain10.xsd";
root.SetAttributeNode(xsiNs);
//root.SetAttribute("xsi:schemaLocation", "http://www.w3.org/2001/XMLSchema-instance");
root.SetAttribute("xmlns:ITR1FORM", "http://incometaxindiaefiling.gov.in/ITR1");
root.SetAttribute("xmlns:ITR2FORM", "http://incometaxindiaefiling.gov.in/ITR2");
root.SetAttribute("xmlns:ITR3FORM", "http://incometaxindiaefiling.gov.in/ITR3");
root.SetAttribute("xmlns:ITR4FORM", "http://incometaxindiaefiling.gov.in/ITR4");
d.AppendChild(root);
d.Save("c://myxml.xml");

and I am getting an output like this

<?xml version="1.0" encoding="utf-8" ?> 
- <!-- Sample XML file generated by XMLSpy v2007 sp2 (http://www.altova.com)
  --> 
- <ITRETURN:ITR xsi:schemaLocation="http://incometaxindiaefiling.gov.in/main ITRMain10.xsd" xmlns:ITR1FORM="http://incometaxindiaefiling.gov.in/ITR1" xmlns:ITR2FORM="http://incometaxindiaefiling.gov.in/ITR2" xmlns:ITR3FORM="http://incometaxindiaefiling.gov.in/ITR3" xmlns:ITR4FORM="http://incometaxindiaefiling.gov.in/ITR4" xmlns:xsi="http://incometaxindiaefiling.gov.in/main ITRMain10.xsd" xmlns:ITRETURN="http://incometaxindiaefiling.gov.in/ITR1">
  <ITR1FORM:ITR1>root node</ITR1FORM:ITR1> 
  </ITRETURN:ITR> 

But my requirement is like

<ITRETURN:ITR xsi:schemaLocation="http://incometaxindiaefiling.gov.in/main ITRMain10.xsd" xmlns:ITR1FORM="http://incometaxindiaefiling.gov.in/ITR1" xmlns:ITR2FORM="http://incometaxindiaefiling.gov.in/ITR2" xmlns:ITR3FORM="http://incometaxindiaefiling.gov.in/ITR3" xmlns:ITR4FORM="http://incometaxindiaefiling.gov.in/ITR4" xmlns:ITRETURN="http://incometaxindiaefiling.gov.in/main" xmlns:ITRForm="http://incometaxindiaefiling.gov.in/master" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+ <ITR1FORM:ITR1>any value
 </ITR1FORM:ITR1>
</ITRETURN:ITR>

What do I need to change in my code to get the desired output?

© Stack Overflow or respective owner

Related posts about c#

Related posts about Xml