0

I am creating an XML document in c#. I want to create this output via XmlWriter. But I am not able to get the exact output.

This is what I need to generate:

<ITRETURN:ITR xmlns:ITRETURN="http://incometaxindiaefiling.gov.in/main" xmlns:ITR1FORM="http://incometaxindiaefiling.gov.in/ITR1" xmlns:ITRForm="http://incometaxindiaefiling.gov.in/master" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <ITR1FORM:ITR1>
  <ITRForm:CreationInfo>
   <ITRForm:SWVersionNo>R1</ITRForm:SWVersionNo>
   <ITRForm:SWCreatedBy>SW92201920</ITRForm:SWCreatedBy>
   <ITRForm:XMLCreatedBy>SW92201920</ITRForm:XMLCreatedBy>
   <ITRForm:XMLCreationDate>2019-07-19</ITRForm:XMLCreationDate>
   <ITRForm:IntermediaryCity>Delhi</ITRForm:IntermediaryCity>
   <ITRForm:Digest>lOa90+jndV+RYN0ghOemod4eomQDqsSm6tw3w8XWsZQ= </ITRForm:Digest>
  </ITRForm:CreationInfo>
 </ITR1FORM:ITR1>
</ITRETURN:ITR>

How can this be generated using XmlWriter?

dbc
  • 104,963
  • 20
  • 228
  • 340

1 Answers1

2

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:

Demo fiddle here.

dbc
  • 104,963
  • 20
  • 228
  • 340