CSharp · Console
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Xml.Serialization;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using System.Data;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO.Compression;
using NUnit.Framework; //NUnit.Framework.AssertionException
//https://stackoverflow.com/questions/57112361/xml-with-namespace-using-xmlwriter
public class TestClass
{
public static void Test()
{
var sb = new StringBuilder();
using (var textWriter = new StringWriter(sb))
{
// 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
}
}
var xml = sb.ToString();
Console.WriteLine(xml);
Assert.IsTrue(XElement.DeepEquals(XElement.Parse(xml), XElement.Parse(GetRequiredXml())));
}
static string GetRequiredXml()
{
var xml = @"<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>";
return xml;
}
}
static class StringExtensions
{
public static int IndexOfFirstDifference(this string a, string b)
{
int index = 0;
int max = Math.Min(a.Length, b.Length);
while (index < max && a[index] == b[index])
index++;
if (index >= a.Length || index >= b.Length)
index = -1;
return index;
}
}
public static class XmlAssert
{
// https://weblogs.asp.net/marianor/easy-way-to-compare-two-xmls-for-equality
// https://stackoverflow.com/questions/7318157/best-way-to-compare-xelement-objects
// Adapted from the answer https://stackoverflow.com/a/14463137 by https://stackoverflow.com/users/1102100/robjohnson
public static void AreEqual(
XElement expected,
XElement actual)
{
Assert.IsTrue(XNode.DeepEquals(Normalize(expected), Normalize(actual)));
}
private static XElement Normalize(XElement element)
{
if (element.HasElements)
{
return new XElement(
element.Name,
element.Attributes()
.OrderBy(a => a.Name.ToString()),
element.Elements()
.OrderBy(a => a.Name.ToString())
.Select(e => Normalize(e)));
}
if (element.IsEmpty)
{
return new XElement(
element.Name,
element.Attributes()
.OrderBy(a => a.Name.ToString()));
}
return new XElement(element.Name, element.Attributes()
.OrderBy(a => a.Name.ToString()), element.Value);
}
}
public class Program
{
public static void Main()
{
Console.WriteLine("Roslyn 2.0 Compiler; Environment version: " + Environment.Version);
Console.WriteLine();
try
{
TestClass.Test();
}
catch (Exception ex)
{
Console.WriteLine("Uncaught exception: ");
Console.WriteLine(ex);
throw;
}
}
}