Remove namespace from XAttribute

47 views Asked by At

It would be really nice to be able to remove the namespace (xmlns:icls="http://www.aade.gr/myDATA/invoice/v1.0") from the classificationType attribute.

Code so far:

public XElement BuildXml() {
    XNamespace ns = "http://www.aade.gr/myDATA/invoice/v1.0";
    var root = new XElement(ns + "InvoicesDoc", new XAttribute("xmlns", ns));
    var x = new XElement(ns + "incomeClassification",
                new XElement(ns + "classificationType",
                    new XAttribute(XNamespace.Xmlns + "icls", ns), "somevalue"));
    root.Add(x);
    return root;
}

Output based on the above code:

<InvoicesDoc xmlns="http://www.aade.gr/myDATA/invoice/v1.0">
    <incomeClassification>
        <icls:classificationType xmlns:icls="http://www.aade.gr/myDATA/invoice/v1.0">somevalue</icls:classificationType>
    </incomeClassification>
</InvoicesDoc>

Desired output should not have the namespace in the attribute:

<InvoicesDoc xmlns="http://www.aade.gr/myDATA/invoice/v1.0">
    <incomeClassification>
        <icls:classificationType>somevalue</icls:classificationType>
    </incomeClassification>
</InvoicesDoc>
1

There are 1 answers

0
John Sourvinos On

As @dbc suggested, I tried xmlTextWriter and worked fine. I built the xml line-by-line.

using XmlTextWriter writer = new("fileName.xml", null);
writer.Namespaces = false;
writer.Formatting = Formatting.Indented;
writer.WriteStartElement("InvoicesDoc");
    writer.WriteAttributeString("xmlns", "http://www.aade.gr/myDATA/invoice/v1.0");
    writer.WriteAttributeString("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
    writer.WriteAttributeString("xmlns:icls", "https://www.aade.gr/myDATA/incomeClassificaton/v1.0");
    writer.WriteAttributeString("xmlns:ecls", "https://www.aade.gr/myDATA/expensesClassificaton/v1.0");
    writer.WriteAttributeString("xsi:schemaLocation", "http://www.aade.gr/myDATA/invoice/v1.0/InvoicesDoc-v0.6.xsd");
    writer.WriteStartElement("invoice");
        writer.WriteStartElement("invoiceSummary");
            writer.WriteElementString("totalNetValue", 1);
            writer.WriteElementString("totalVatAmount", 0.24);
            writer.WriteElementString("totalWithheldAmount", 0);
            writer.WriteElementString("totalFeesAmount", 0);
            writer.WriteElementString("totalStampDutyAmount", 0);
            writer.WriteElementString("totalOtherTaxesAmount", 0);
            writer.WriteElementString("totalDeductionsAmount", 0);
            writer.WriteElementString("totalGrossValue", 1.24);
            writer.WriteStartElement("incomeClassification");
                writer.WriteElementString("icls:classificationType", "Description A");
                writer.WriteElementString("icls:classificationCategory", "Description B");
                writer.WriteElementString("icls:amount", "Description C");
            writer.WriteEndElement();
        writer.WriteEndElement();
    writer.WriteEndElement();
writer.WriteEndElement();
writer.Close();