C# 使用c使用XElement创建XML文档#

C# 使用c使用XElement创建XML文档#,c#,xml,C#,Xml,我有一个c#类,它继承自另一个对象,其中包含字符串列表。结构与此类似: public class BeneficiaryScreening { public int Id { get; set; } public DateTime ProcessedDate { get; set; } public string FileName { get; set; } public int LineCountFile { get; set; } public int

我有一个c#类,它继承自另一个对象,其中包含字符串列表。结构与此类似:

public class BeneficiaryScreening
{
    public int Id { get; set; }
    public DateTime ProcessedDate { get; set; }
    public string FileName { get; set; }
    public int LineCountFile { get; set; }
    public int LineCountHits { get; set; }
    public string SearchLine { get; set; }
    public Entity Entity { get; set; }

}
[Serializable]
public class Entity
{  
    public string MessageId { get; set; }
    public string Id { get; set; }
    public string ItemType { get; set; }
    public string DateListed { get; set; }
    public string Gender { get; set; }
    public List<string> Names { get; set; }
    public List<string> Countries { get; set; }
}
我的问题是,一切看起来都很好,直到我开始循环遍历名称,这会产生以下结果:

<Entity>
  <Names>
    <name>
      <name>Some Name</name>
      <name>Some Name</name>
    </name>
  </Names>
</Entity>

因此,我只需要在值之间留出一个空格,您只需为每个名称创建一个名为
value
的新
XElement

select new XElement("name", new XElement("value", name))
顺便说一句,我不会为此使用查询表达式-我只会使用:

new XElement("Entity",
    new XElement("Names",
        new XElement("name",
            bene.Entity.Names
                .Select(name => new XElement("name", new XElement("value", name))
        )
    )

您只需为每个名称创建一个名为
value
的新
XElement

select new XElement("name", new XElement("value", name))
顺便说一句,我不会为此使用查询表达式-我只会使用:

new XElement("Entity",
    new XElement("Names",
        new XElement("name",
            bene.Entity.Names
                .Select(name => new XElement("name", new XElement("value", name))
        )
    )

您只需为每个名称创建一个名为
value
的新
XElement

select new XElement("name", new XElement("value", name))
顺便说一句,我不会为此使用查询表达式-我只会使用:

new XElement("Entity",
    new XElement("Names",
        new XElement("name",
            bene.Entity.Names
                .Select(name => new XElement("name", new XElement("value", name))
        )
    )

您只需为每个名称创建一个名为
value
的新
XElement

select new XElement("name", new XElement("value", name))
顺便说一句,我不会为此使用查询表达式-我只会使用:

new XElement("Entity",
    new XElement("Names",
        new XElement("name",
            bene.Entity.Names
                .Select(name => new XElement("name", new XElement("value", name))
        )
    )

你不是有点多余吗?姓名标签内的姓名?这就是你真正想要的吗?还是简单地在名称中命名元素列表

XElement element =
            new XElement("Root",
                (from bene in xmlDataSet
                    select new XElement("Item",
                            new XElement("Id", bene.Id),
                            new XElement("ProcessedDate", bene.ProcessedDate),
                            new XElement("FileName", bene.FileName),
                            new XElement("LineCountFile", bene.LineCountFile),
                            new XElement("LineCountHits", bene.LineCountHits),
                            new XElement("Line", bene.SearchLine),
                        new XElement("Entity",
                                new XElement("Names",
                                from name in bene.Entity.Names
                                select new XElement("name", name))))
                            )
                    );

你不是有点多余吗?姓名标签内的姓名?这就是你真正想要的吗?还是简单地在名称中命名元素列表

XElement element =
            new XElement("Root",
                (from bene in xmlDataSet
                    select new XElement("Item",
                            new XElement("Id", bene.Id),
                            new XElement("ProcessedDate", bene.ProcessedDate),
                            new XElement("FileName", bene.FileName),
                            new XElement("LineCountFile", bene.LineCountFile),
                            new XElement("LineCountHits", bene.LineCountHits),
                            new XElement("Line", bene.SearchLine),
                        new XElement("Entity",
                                new XElement("Names",
                                from name in bene.Entity.Names
                                select new XElement("name", name))))
                            )
                    );

你不是有点多余吗?姓名标签内的姓名?这就是你真正想要的吗?还是简单地在名称中命名元素列表

XElement element =
            new XElement("Root",
                (from bene in xmlDataSet
                    select new XElement("Item",
                            new XElement("Id", bene.Id),
                            new XElement("ProcessedDate", bene.ProcessedDate),
                            new XElement("FileName", bene.FileName),
                            new XElement("LineCountFile", bene.LineCountFile),
                            new XElement("LineCountHits", bene.LineCountHits),
                            new XElement("Line", bene.SearchLine),
                        new XElement("Entity",
                                new XElement("Names",
                                from name in bene.Entity.Names
                                select new XElement("name", name))))
                            )
                    );

你不是有点多余吗?姓名标签内的姓名?这就是你真正想要的吗?还是简单地在名称中命名元素列表

XElement element =
            new XElement("Root",
                (from bene in xmlDataSet
                    select new XElement("Item",
                            new XElement("Id", bene.Id),
                            new XElement("ProcessedDate", bene.ProcessedDate),
                            new XElement("FileName", bene.FileName),
                            new XElement("LineCountFile", bene.LineCountFile),
                            new XElement("LineCountHits", bene.LineCountHits),
                            new XElement("Line", bene.SearchLine),
                        new XElement("Entity",
                                new XElement("Names",
                                from name in bene.Entity.Names
                                select new XElement("name", name))))
                            )
                    );

请参见编辑Jon,它从您的recommendation@CSharpNewBee:你应该把这个问题作为一个新问题来问——塞廷和我都回答了你提出的问题,而持续不断的“你回答了我关于X的问题,但我真的想要Y”对堆栈溢出不起作用。虽然它与这个问题有关,但我明白你的观点。Cheers@CSharpNewBee:因此,在新问题中加入指向此问题的链接-但仍要使新问题独立,最好是用一个简短但完整的程序演示您迄今为止的尝试、结果以及您的预期。请参见编辑Jon,从你的recommendation@CSharpNewBee:你应该把这个问题作为一个新问题来问——塞廷和我都回答了你提出的问题,而持续不断的“你回答了我关于X的问题,但我真的想要Y”对堆栈溢出不起作用。虽然它与这个问题有关,但我明白你的观点。Cheers@CSharpNewBee:因此,在新问题中加入指向此问题的链接-但仍要使新问题独立,最好是用一个简短但完整的程序演示您迄今为止的尝试、结果以及您的预期。请参见编辑Jon,从你的recommendation@CSharpNewBee:你应该把这个问题作为一个新问题来问——塞廷和我都回答了你提出的问题,而持续不断的“你回答了我关于X的问题,但我真的想要Y”对堆栈溢出不起作用。虽然它与这个问题有关,但我明白你的观点。Cheers@CSharpNewBee:因此,在新问题中加入指向此问题的链接-但仍要使新问题独立,最好是用一个简短但完整的程序演示您迄今为止的尝试、结果以及您的预期。请参见编辑Jon,从你的recommendation@CSharpNewBee:你应该把这个问题作为一个新问题来问——塞廷和我都回答了你提出的问题,而持续不断的“你回答了我关于X的问题,但我真的想要Y”对堆栈溢出不起作用。虽然它与这个问题有关,但我明白你的观点。Cheers@CSharpNewBee:因此,在新问题中加入一个指向此问题的链接-但仍要使新问题独立,最好是用一个简短但完整的程序演示您迄今为止的尝试、结果以及您的预期。