【发布时间】:2015-10-13 14:37:32
【问题描述】:
我正在尝试获取将数据转换为 XML 的链接。 我几乎可以使用的 LINQ 表达式是:
XElement xml = new XElement("contacts",
lstEmailData.Select(i => new XElement("Data",
new XAttribute("URL", i.WebPage ),
new XAttribute("emails", i.Emails.ToArray() + " , ")
)));
其中 lstEmailData 定义为:
List<PageEmail> lstEmailData = new List<PageEmail>();
lstEmailData.Add(new PageEmail("site2", new List<string>() {
"MyHotMail@NyTimes.com", "contact_us@ml.com" }));
PageEmail 在哪里:
class PageEmail
{
public string WebPage { get; set; }
public List<string> Emails { get; set; }
public PageEmail(string CurWebPage, List<string> CurEmails)
{
this.WebPage = CurWebPage;
this.Emails = CurEmails;
}
}
LINQ 的 XML 输出已关闭,我没有收到电子邮件列表:
<contacts>
<Data URL="site1" emails="System.String[] , " />
<Data URL="site2" emails="System.String[] , " />
</contacts>
如何让每个 i.Email 进入它们自己的 xml 节点?
【问题讨论】:
-
XElement xml = new XElement("SiteData", lstEmailData.Select(i => new XElement("WebSite", new XAttribute("URL", i.WebPage), from o in i.Emails) select new XElement("mail", o.ToString() ) )));
标签: c# linq linq-to-xml