如何使用 linq 将字符串列表 <t> 转换为 XML
how to use linq to convert list of string,<t> into XML
我正在尝试让 link 将数据转换为 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>
How to get each of the i.Emails into their own xml nodes?
我猜您正试图将所有电子邮件存储在 emails
属性中。
使用 String.Join:-
new XAttribute("emails", String.Join(",", i.Emails)
当您将对象作为第二个参数传递给 XAttribute
构造函数时。它在其上调用 ToString
方法。在数组上调用 ToString
的结果是数组的名称(因此您得到 System.String[]
)要显示其中的字符串,您应该改用 String.Join
。
XElement xml = new XElement("contacts",
lstEmailData.Select(i => new XElement("Data",
new XAttribute("URL", i.WebPage ),
new XAttribute("emails", String.Join(",", i.Emails))
)));
How to get each of the i.Emails into their own xml nodes?
Try this:
XElement xml = new XElement("contacts",
lstEmailData.Select(pageEmail =>
new XElement("Data", new XAttribute("Url",pageEmail.WebPage),
pageEmail.Emails.Select(email => new XElement("Email",email))
)
)
);
结果:
<contacts>
<Data Url="site2">
<Email>MyHotMail@NyTimes.com</Email>
<Email>contact_us@ml.com</Email>
</Data>
</contacts>
我正在尝试让 link 将数据转换为 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>
How to get each of the i.Emails into their own xml nodes?
我猜您正试图将所有电子邮件存储在 emails
属性中。
使用 String.Join:-
new XAttribute("emails", String.Join(",", i.Emails)
当您将对象作为第二个参数传递给 XAttribute
构造函数时。它在其上调用 ToString
方法。在数组上调用 ToString
的结果是数组的名称(因此您得到 System.String[]
)要显示其中的字符串,您应该改用 String.Join
。
XElement xml = new XElement("contacts",
lstEmailData.Select(i => new XElement("Data",
new XAttribute("URL", i.WebPage ),
new XAttribute("emails", String.Join(",", i.Emails))
)));
How to get each of the i.Emails into their own xml nodes? Try this:
XElement xml = new XElement("contacts",
lstEmailData.Select(pageEmail =>
new XElement("Data", new XAttribute("Url",pageEmail.WebPage),
pageEmail.Emails.Select(email => new XElement("Email",email))
)
)
);
结果:
<contacts>
<Data Url="site2">
<Email>MyHotMail@NyTimes.com</Email>
<Email>contact_us@ml.com</Email>
</Data>
</contacts>