【问题标题】:Creating an XML document with XElement using c#使用 C# 使用 XElement 创建 XML 文档
【发布时间】:2015-08-03 16:39:53
【问题描述】:

我有一个 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 以用于另一个应用程序。

如果我只使用单值字段,我可以很好地创建文档,但我无法为所有列表项创建正确的 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)))))
                            ))
                    );

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

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

谁能指出我能够创建列表项的方向,如下所示:

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

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

【问题讨论】:

    标签: c# xml


    【解决方案1】:

    您只需为每个名称创建一个名为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))
            )
        )
    

    【讨论】:

    • 查看 Jon 的编辑,继续您的推荐
    • @CSharpNewBee:您应该将其作为一个新问题提出 - Cetin 和我都回答了​​您提出的问题,并且持续不断地“嗯,您回答了我关于 X 的问题,但我真的想要 Y”没有在 Stack Overflow 上运行良好。
    • 它虽然与这个问题有关,但我确实明白你的意思。干杯
    • @CSharpNewBee:所以在新问题中包含一个指向这个问题的链接 - 但仍然让新问题也独立存在,理想情况下有一个简短但完整的程序来展示你迄今为止尝试过的内容,什么结果是,和你期望的一样。
    【解决方案2】:

    你那里没有一点冗余吗?名称标签内的名称?那是你真正想要的吗?还是简单地在名称中命名元素列表?

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-21
      • 2013-04-24
      相关资源
      最近更新 更多