代码之家  ›  专栏  ›  技术社区  ›  Nastya

如何在xml中进行检查?

  •  0
  • Nastya  · 技术社区  · 6 年前

    我有创建xml的方法:

    var xml = new XmlDocument();
    
    var head = xml.CreateElement("Head");
    var uniqueIdentifier = xml.CreateElement("Unique_identifier");
    var documentDate = xml.CreateElement("Document_date");
    var documentNumber = xml.CreateElement("Document_number");
    
    uniqueIdentifier.InnerText = "1";
    documentDate.InnerText = "2019-01-01";
    documentNumber.InnerText = "2";
    
    xml.AppendChild(head);
    head.AppendChild(uniqueIdentifier);
    head.AppendChild(documentDate);
    head.AppendChild(documentNumber);
    

    从db获取的InnerText值,此字段可以为空。因此,需要检查每个变量是否为null和空字符串

        var uniqueIdentifier = xml.CreateElement("Unique_identifier");
        var r = reader.GetFieldValue("Unique_identifier");
        if (r is string)
        {
            if (!string.IsNullOrWhiteSpace(r))
            {
                uniqueIdentifier.InnerText = Text.Convert(r);
            }
        }
    

    我得到了很多额外的行(5个变量和10个检查)。有多干净?

    0 回复  |  直到 6 年前
    推荐文章