0

我构建了这个 xml 文件:

   var persons = new[] {
   new Person {
      Name = "Patrick Hines",
      PhoneNumbers = new[] { "206-555-0144", "425-555-0145" }
   },
   new Person {
      Name = "Gretchen Rivas",
      PhoneNumbers = new[] { "206-555-0163" }
   }
};
            XElement contacts =
               new XElement("contacts",
                  from p in persons
                  where p.Name.StartsWith("G")
                  select new XElement("contact",
                     new XElement("name", p.Name),
                     from ph in p.PhoneNumbers
                     select new XElement("phone", ph)
                  )
               );


        class Person
        {
            public string Name;
            public string[] PhoneNumbers;
        }

在此示例中,电话号码不为空。当电话号码字符串为空或为空时,如何编码它不会在我的 xml 中创建元素?

4

1 回答 1

0

从 PhoneNumbers 中过滤掉 null 和空字符串:

   from ph in p.PhoneNumbers where !String.IsNullOrEmpty(ph)
于 2013-06-12T02:35:08.727 回答