C# 如何在c语言中使用Xelement中的foreach循环

C# 如何在c语言中使用Xelement中的foreach循环,c#,xml,xelement,C#,Xml,Xelement,基本上content.attachment是附件名称的列表,我想在body元素之后添加该列表。如何做到这一点 您正在向根元素添加附件,而不是添加电子邮件元素。你可以像下面这样做 XElement doc = null; doc = new XElement("root"); foreach (var content in emailContents) { doc.Add(new XElement("Email"

基本上content.attachment是附件名称的列表,我想在body元素之后添加该列表。如何做到这一点

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

 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)        
        }
}

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

 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)        
        }
}

一次完成相当容易:

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)        
    }
我从以下示例数据开始:

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)))));
我得到了这个XML:

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

一次完成相当容易:

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)        
    }
我从以下示例数据开始:

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)))));
我得到了这个XML:

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

只需分配var email=new XElementEmail。。。;然后将其添加到文档中,然后将附件添加到文档中?只需分配var email=new XElementEmail。。。;然后将其添加到文档中,然后将附件添加到文档中?我发现此错误->对于物化查询结果不支持此方法。@rohitsingh如果列表是来自数据库的查询,在使用Select之前,请尝试对其使用.ToList。我发现此错误->具体化查询结果不支持此方法。@rohitsingh如果列表是来自数据库的查询,请在使用Select之前尝试对其使用.ToList。