如果值为 0,则不添加 XElement

Do not add XElement if value is 0

我正在创建一个 XML(见下面的函数):

public static string CreteInvoice()
        {
            string _dc;

            Mark pm = new Mark();
            XNamespace tu = "http://www.name.com";

            var item = QueryDb.InvoiceHeader;
            var TaxNr = QueryDb.MyCertificates.FirstOrDefault();
            _dc = TaxNr.CertificateTaxNumber;

            XElement root = new XElement(tu + "InvoiceRequest",
                                new XAttribute(XNamespace.Xmlns + "tu", "http://www.test.com/"),
                                new XAttribute("Id", "data"),
                                new XElement(tu + "Header",
                                    new XElement(tu + "MessageID", item.XMLMessageID),
                                    new XElement(tu + "DateTime", item.DateSentToFurs.ToString("yyyy-MM-ddTHH:mm:ss"))),
                                    new XElement(tu + "Invoice",
                                        new XElement(tu + "TaxNumber", _dc),
                                        new XElement(tu + "IssueDateTime", item.DateOfInvoiceCreation.ToString("yyyy-MM-ddTHH:mm:ss")),
                                        new XElement(tu + "NumberingStructure", item.NumberingStructure),
                                        new XElement(tu + "InvoiceIdentifier",
                                            new XElement(tu + "BusinessPremiseID", item.BussinesspremiseID),
                                            new XElement(tu + "ElectronicDeviceID", item.ElectronicDeviceID),
                                            new XElement(tu + "InvoiceNumber", item.InvoiceNumber) 
                                        //  ,new XElement(tu + "TaxNrSeller", item.TaxNrSeller) <- problem when item.TaxNrSeller is 0 or empty
                                        )
                                    ));

            XDocument xdoc = new XDocument(new XDeclaration("1.0", "UTF-8", ""));
            xdoc.Add(root);

            string xdocText = xdoc.Declaration.ToString().Remove(36, 14) + "\n" + xdoc.ToString();

            return xdocText;
        }

在上面的代码中,我标记了我应该只创建 XElement 的位置: 如果 item.TaxNrSeller 不同于 0。 如果 item.TaxNrSeller0,则不应应用标签 TaxNrSeller

如果您需要更多信息,请告诉我!

这样试试:

new XElement(tu + "InvoiceNumber", item.InvoiceNumber),
item.TaxNrSeller != null && item.TaxNrSeller != 0 ? new XElement(tu + "TaxNrSeller", item.TaxNrSeller) : null

创建一个新方法,稍微重构您的代码以创建 Xelement 节点:

public static XElement CreateNode(XNamespace nameSpace, string nodeName, object value)
{
    return new XElement(nameSpace + nodeName, value);
}

那么在你的方法中就可以使用了:

XElement root = new XElement(tu + "InvoiceRequest",
                    new XAttribute(XNamespace.Xmlns + "tu", "http://www.test.com/"),
                    new XAttribute("Id", "data"),
                    new XElement(tu + "Header"
                        ));
root.Add(CreateNode(tu, "MessageID", item.XMLMessageID));
// etc...
if (item.TaxNrSeller != 0 || item.TaxNrSeller != null)
    root.Add(CreateNode(tu, "TaxNrSeller", item.TaxNrSeller));

事实证明,如果您传递一个空元素,它就会被跳过。所以你应该没问题:

new XElement(tu + "InvoiceIdentifier",
    new XElement(tu + "BusinessPremiseID", item.BussinesspremiseID),
    new XElement(tu + "ElectronicDeviceID", item.ElectronicDeviceID),
    new XElement(tu + "InvoiceNumber", item.InvoiceNumber),
    item.TaxNrSeller != 0 ? new XElement(tu + "TaxNrSeller", item.TaxNrSeller) : null
);

If content is null, nothing is added. When passing a collection items in the collection can be null. A null item in the collection has no effect on the tree.

Source: https://msdn.microsoft.com/en-us/library/bb943882(v=vs.110).aspx

除此之外,您可以手动构建对象(这样更容易调试)。通过这种方式,您可以提供 0 检查自己。