【问题标题】:How to use foreach loop inside a Xelement in c#如何在 C# 中的 Xelement 中使用 foreach 循环
【发布时间】:2015-07-10 04:38:08
【问题描述】:
 XElement doc = null;
 doc = new XElement("root");
 foreach (var content in emailContents)
                                    {
    doc.Add(new XElement("Email",
            new XElement("FromAddress", content.FromAddress),
            new XElement("EmailReceivedOn", content.receivedOnemail),
            new XElement("Subject", content.subject),
            new XElement("Body", content.body)));
    // I want to add the below code after the body element is created in the xml inside the email element section; How to do the same?
     foreach (var attachment in content.attachments)
        {
          doc.Add(new XElement("attachmentname"), attachment.Filename),
          doc.Add(new XElement(("attachmentpath"), attachment.Filepath)        
        }
}

基本上 content.attachment 是附件名称列表,我想在正文元素之后添加列表。如何做同样的事情?

【问题讨论】:

  • 只需分配var email = new XElement("Email", ...);,然后添加到doc,然后添加附件?

标签: c# xml xelement


【解决方案1】:

一气呵成很简单:

var doc =
    new XElement("root",
        new XElement("Email",
            new XElement("FromAddress", content.FromAddress),
            new XElement("EmailReceivedOn", content.receivedOnemail),
            new XElement("Subject", content.subject),
            new XElement("Body", content.body),
            content.attachments.Select(attachment =>
                new XElement("attachment",
                    new XElement("attachmentname", attachment.Filename),
                    new XElement("attachmentpath", attachment.Filepath)))));

我从这个示例数据开始:

var content = new
{
    FromAddress = "FromAddress",
    receivedOnemail = "receivedOnemail",
    subject = "subject",
    body = "body",
    attachments = new []
    {
        new
        {
            Filename = "Filename",
            Filepath = "Filepath",
        },
    },
};

我得到了这个 XML:

<root>
  <Email>
    <FromAddress>FromAddress</FromAddress>
    <EmailReceivedOn>receivedOnemail</EmailReceivedOn>
    <Subject>subject</Subject>
    <Body>body</Body>
    <attachment>
      <attachmentname>Filename</attachmentname>
      <attachmentpath>Filepath</attachmentpath>
    </attachment>
  </Email>
</root>

【讨论】:

  • 我收到此错误 --> 物化查询结果不支持此方法。
  • @rohitsingh 如果列表是来自数据库的查询,请在使用 Select() 之前尝试对其使用 .ToList()
【解决方案2】:

您正在向根元素添加附件,而不是在电子邮件元素中添加。你可以像下面那样做。

XElement doc = null;
doc = new XElement("root");
doc.Add(new XElement("Email",
        new XElement("FromAddress", content.FromAddress),
        new XElement("EmailReceivedOn", content.receivedOnemail),
        new XElement("Subject", content.subject),
        new XElement("Body", content.body)));
 var email=doc.Element("Email"); // get email element from root Element
 foreach (var attachment in content.attachments)
    {
      //Add attachemnt information to email element.
      email.Add(new XElement("attachmentname"), attachment.Filename),
      email.Add(new XElement(("attachmentpath"), attachment.Filepath)        
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多