![](/img/trans.png)
[英]StyleCop happy creation of Xml using XDocument / XElement / XAttribute
[英]xml conditional XElement creation
我建立這個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;
}
在此示例中,電話號碼不為null。 當電話號碼字符串為空或null時,如何在XML中不會創建元素的方式編碼?
從電話號碼中過濾出空字符串:
from ph in p.PhoneNumbers where !String.IsNullOrEmpty(ph)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.