I am creating an ASP.NET Core Web API endpoint which will fetch XML output in the Nautix compliant XML. Despite the XmlAttribute set on the class, it does not return attribute in the output. Please suggest what I am doing wrong.
I am using this one in the controller to generate the xml output.
[Produces("application/xml")]
Method being called to get the XML:
private ActionResult GetXmlResponse()
{
var sample = new Nautix
{
Xmlns = "https://nautix.nautic-network.org/v1.3/",
Xsi = "http://www.w3.org/2001/XMLSchema-instance",
Version = "1.3",
Origin = "Api.Catamarans.com",
Date = DateTime.Now,
SchemaLocation = "https://nautix.nautic-network.org/v1.3/ https://nautix.nautic-network.org/v1.3/NautiX.xsd",
Account = new Account
{
Token = "101",
AccountName = "Catamarans.com"
}
};
return new ContentResult
{
Content = sample.ToString(),
ContentType = "application/xml",
StatusCode = StatusCodes.Status200OK
};
}
Sample class with XML decoration:
[XmlRoot(ElementName = "nautix")]
public class Nautix
{
[XmlAttribute(AttributeName = "xmlns")]
public string Xmlns { get; set; }
[XmlAttribute(AttributeName = "xsi")]
public string Xsi { get; set; }
[XmlAttribute(AttributeName = "version")]
public string Version { get; set; }
[XmlAttribute(AttributeName = "origin")]
public string Origin { get; set; }
[XmlAttribute(AttributeName = "date")]
public DateTime Date { get; set; }
[XmlAttribute(AttributeName = "schemaLocation")]
public string SchemaLocation { get; set; }
[XmlElement(ElementName = "script")]
public List<Script> Script { get; set; }
[XmlElement(ElementName = "account")]
public Account Account { get; set; }
[XmlText]
public string Text { get; set; }
}
I have tried different ways, but without success so far.
var xmlDoc = new XmlDocument();
// Create the header of the response
xmlDoc.AppendChild(xmlDoc.CreateXmlDeclaration("1.0", "utf-8", string.Empty));
// Create the response body
var rootNode = xmlDoc.CreateElement("NautiX");
xmlDoc.AppendChild(rootNode);
rootNode.SetAttribute("xmlns", nautix.Xmlns);
rootNode.SetAttribute("xmlns:xsi", nautix.Xsi);
rootNode.SetAttribute("version", nautix.Version);
rootNode.SetAttribute("origin", nautix.Origin);
rootNode.SetAttribute("date", nautix.Date.ToString("yyyy-MM-ddTHH:mm:ssZ"));
rootNode.SetAttribute("xsi:schemaLocation", nautix.SchemaLocation);
var accountNode = xmlDoc.CreateElement("Account");
rootNode.AppendChild(accountNode);
var tokenNode = xmlDoc.CreateElement("Token");
tokenNode.InnerText = nautix.Account.Token;
accountNode.AppendChild(tokenNode);
var accountNameNode = xmlDoc.CreateElement("AccountName");
accountNameNode.InnerText = nautix.Account.AccountName;
accountNode.AppendChild(accountNameNode);
My goal is to get the output in the XML specified with XmlAttribute, XmlElement & XmlRoot being honoured as defined in the class definition.
I might be doing something wrong. I always worked with JSON output, this is the first time I'm working with XML.
I have added this code in ConfigureServices in Startup.cs:
services.AddControllers()
.AddXmlDataContractSerializerFormatters();
A complete sample XML file is here: