You can write that XML using the following code:
// Namespace constants
var ITRETURN = @"http://incometaxindiaefiling.gov.in/main";
var ITR1FORM = @"http://incometaxindiaefiling.gov.in/ITR1";
var ITRForm = @"http://incometaxindiaefiling.gov.in/master";
var xsi = @"http://www.w3.org/2001/XMLSchema-instance";
// Enable indenting and disable the XML declaration.
var settings = new XmlWriterSettings
{
Indent = true,
IndentChars = " ",
OmitXmlDeclaration = true,
};
using (var xmlWriter = XmlWriter.Create(textWriter, settings))
{
// Write the start element and all namespaces.
xmlWriter.WriteStartElement("ITRETURN", "ITR", ITRETURN);
xmlWriter.WriteAttributeString("xmlns","ITRETURN", null, ITRETURN);
xmlWriter.WriteAttributeString("xmlns","ITR1FORM", null, ITR1FORM);
xmlWriter.WriteAttributeString("xmlns","ITRForm", null, ITRForm);
xmlWriter.WriteAttributeString("xmlns","xsi", null, xsi);
// Write the ITR1 element
xmlWriter.WriteStartElement("ITR1", ITR1FORM);
// Write the CreationInfo
xmlWriter.WriteStartElement("CreationInfo", ITRForm);
xmlWriter.WriteElementString("SWVersionNo", ITRForm, "R1");
xmlWriter.WriteElementString("SWCreatedBy", ITRForm, "SW92201920");
xmlWriter.WriteElementString("XMLCreatedBy", ITRForm, "SW92201920");
xmlWriter.WriteElementString("XMLCreationDate", ITRForm, "2019-07-19");
xmlWriter.WriteElementString("IntermediaryCity", ITRForm, "Delhi");
xmlWriter.WriteElementString("Digest", ITRForm, "lOa90+jndV+RYN0ghOemod4eomQDqsSm6tw3w8XWsZQ= ");
xmlWriter.WriteEndElement(); // CreationInfo
xmlWriter.WriteEndElement(); // ITR1
xmlWriter.WriteEndElement(); // ITR
}
Notes:
In the above code textWriter
is some StreamWriter
or StringWriter
to which the XML is being written.
When directly writing XML with elements that are in specified namespaces, you should generally avoid hardcoding the namespace prefix. Instead, use XmlWriter
methods that take the local name and full namespace such as XmlWriter.WriteStartElement(string localName, string ns)
or XmlWriter.WriteElementString(string localName, string ns, string value)
.
If you do need to hardcode some prefixes, see this answer to Adding multiple namespace declarations in XmlWriter by Ryan B for instructions how to write the necessary attribute(s). You will also need to manually write a prefix for the element on which the namespace is declared (here the root element) with XmlWriter.WriteStartElement(string prefix, string localName, string ns)
. But once the prefixes have been defined, all child elements will pick them up automatically.
(It should not be necessary to hardcode the prefixes. As long as the receiving system parses the XML namespaces properly the prefixes used should not matter. Of course not all receiving systems parse XML properly...)
I indented the XML and omitted the XML declaration as shown in your question.
If Digest
is actually a byte array in your production code, you can write it out easily as Base64 using XmlWriter.WriteBase64(byte[] buffer, int index, int count)
.
Demo fiddle here.