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

时间:2015-08-03 16:39:54

标签: 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 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; }
}

我使用Dapper填充类,现在需要将结果转换为XML以与其他应用程序一起使用。

如果我只使用sinlge值字段,我可以很好地创建文档,但是我无法为所有列表项创建正确的XML。

我到目前为止的代码是:

 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",new XElement("name", (from name in bene.Entity.Names
                                select new XElement("name", name)))))
                            ))
                    );

我的问题是,一切看起来都很好,直到我开始遍历Names,产生以下内容:

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

有人能指出我能够创建List项目的方向,如下所示:

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

更新 好的,你的例子都是关于创建XML的,因此投票 - 但是,我的外部程序无法遍历子节点。所以我需要在一个XElement中创建每个Name / Country,用逗号分隔。

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

所以,我需要的只是值之间的空格

2 个答案:

答案 0 :(得分:2)

您只需为每个名称创建一个名为POST /v3/users/6/reset_password HTTP/1.1 Host: localhost:8080 Content-Type: application/json Cache-Control: no-cache Postman-Token: 8afe6ef8-a4cd-fc9d-a6cc-b92766a56bd6 {"old_password":"actualPassword", "new_password":"newOne"} 的新XElement

value

顺便说一句,我不会使用查询表达式 - 我只是使用:

select new XElement("name", new XElement("value", name))

答案 1 :(得分:1)

那不是你有点冗余吗?名称标签内的名称?那是你真正想要的吗?或者只是在名称中命名元素列表?

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