如果value为0,请不要添加XElement

时间:2016-03-18 10:11:02

标签: c# xml xelement

我正在创建一个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.TaxNrSeller0不同。 如果item.TaxNrSeller0,则不应该应用TaxNrSeller

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

3 个答案:

答案 0 :(得分:2)

试试这样:

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

答案 1 :(得分:2)

原来,如果传递一个null元素,则跳过它。所以你应该没问题:

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
);
  

如果内容为 null ,则不会添加任何内容。传递集合时,集合中的项目可以是 null 。集合中的 null 项对树没有影响。

     

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

除此之外,您可以手动构建对象(这更容易调试)。这样您就可以自己进行0检查。

答案 2 :(得分:1)

创建一个新方法,稍微重构一下代码以创建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));