如果能够从
xmlns:icls="http://www.aade.gr/myDATA/invoice/v1.0"
属性中删除命名空间 (classificationType
),那就太好了。
到目前为止的代码:
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;
}
基于上述代码的输出:
<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>
所需的输出不应在属性中包含名称空间:
<InvoicesDoc xmlns="http://www.aade.gr/myDATA/invoice/v1.0">
<incomeClassification>
<icls:classificationType>somevalue</icls:classificationType>
</incomeClassification>
</InvoicesDoc>
正如 @dbc 所建议的,我尝试了 xmlTextWriter 并且工作正常。我逐行构建了 xml。
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();