1

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?

2
  • 1
    what are you getting ? Commented Jul 19, 2019 at 12:21
  • 1
    Please share code you did and the result you are receiving. Commented Jul 19, 2019 at 12:35

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.