我构建了这个 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 中创建元素?