【问题标题】:LINQ to XML - How to use XDocument the right wayLINQ to XML - 如何正确使用 XDocument
【发布时间】:2013-05-28 15:05:06
【问题描述】:

现在我将首先说这确实是一个任务。然而,在我遇到 Linq to XML 语法之前,我几乎完成了它。

我有 2 个类:轨道和 CD 现在作为分配的一部分,我创建了一个 cd,然后向它添加了一些轨道。在搜索了许多完美解释如何从 xml 到对象的教程之后,我似乎无法让它工作(对象到 xml)。

我目前有:

//My list of cds
List<CD> cds = new List<CD>();
//Make a new CD and add some tracks to it
CD c1 = new CD("Awake","Dream Theater");
Track t1 = new Track("6:00", "Dream Theater", new TimeSpan(00, 05, 31));
Track t2 = new Track("Caught in a Web", "Dream Theater", new TimeSpan(00, 05, 28));
Track t3 = new Track("Innocence Faded", "Dream Theater", new TimeSpan(00, 05, 34));
c1.addTrack(t1);
c1.addTrack(t2);
c1.addTrack(t3);
cds.Add(c1);

//Make another cd and add it
CD c2 = new CD("Second cd","TestArtist");
Track t4 = new Track("TrackForSecond","TestArtist",new TimeSpan(00,13,37));
c2.addTrack(t4);
cds.add(c2);

现在这就是我需要放入 XML 的对象的原因。 to XML 部分是:

XDocument xmlOutput = new XDocument (
     new XDeclaration("1.0","utf-8","yes"),
     (from cl in cds orderby cl.getArtist()
        select new XElement("cd",  /*From new to the end of this is the error*/
            (
               from c in cds
                   select new XAttribute("artist",c.getArtist())
            ),
            (
               from c in cds
                   select new XAttribute("name", c.getTitle())
            ),
            new XElement("tracks",
               (
                   from t in c1.getTracks()
                       select new XElement("track",
                           new XElement("artist",t1.getArtist()),    
                           new XElement("title",t1.getTitle()),
                           new XElement("length",t1.getLength())
                       )          
               )                    
            )
        )
    )                   
);
Console.WriteLine(xmlOutput);

只需 1 cd,效果就很好(得到了我需要的结果!)。当我决定添加另一张 cd 时,它显示:

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Xml.Linq.dll
Duplicate Attribute (cd)

指向 XDocument。除了这不起作用之外,它感觉很愚蠢(来自 cds x2 中的 c),但无论我尝试什么,我似乎都无法阻止这种语法讨厌我:

(
from c in cds
   select new XAttribute("artist",c.getArtist()),    
   select new XAttribute("name", c.getTitle()) //No not happening!
),

很高兴您能提供任何帮助!

【问题讨论】:

  • 为什么不直接使用XmlSerializer 类,然后将结果解析为XDocument
  • @LukeHennerley 我没听说过,老师在展示这种结构。
  • 这看起来是提出家庭作业问题的好方法。但您似乎错过了异常发生在哪一行?
  • @Sayse 我更新了问题以显示引发异常的行
  • 尝试在嵌套选择上设置断点,看看是否可以将其缩小一点..

标签: c# xml linq linq-to-xml


【解决方案1】:

首先,我建议您对方法使用属性和 C# 样式命名。以下是您的类可以如何重构:

public class CD
{
    private readonly List<Track> _tracks = new List<Track>();

    public CD(string artist, string title)
    {
        Artist = artist;
        Title = title;
    }

    public string Artist { get; private set; }
    public string Title { get; private set; }

    public  IEnumerable<Track> Tracks
    {
        get { return _tracks; }
    } 

    public void AddTrack(Track track)
    {
        _tracks.Add(track);
    }

    public CD WithTrack(string title, TimeSpan length)
    {
        AddTrack(new Track(Artist, title, length));
        return this;
    }
}

这是Value Object 类 - 私有设置器不允许更改此类之外的属性值。这是轨道类:

public class Track
{
    public Track(string artist, string title, TimeSpan length)
    {
        Artist = artist;
        Title = title;
        Length = length;
    }

    public string Artist { get; set; }
    public string Title { get; private set; }
    public TimeSpan Length { get; private set; }
}

现在您可以使用Fluent API 来创建 CD 收藏:

List<CD> cds = new List<CD>
    {
        new CD("Awake", "Dream Theater")
            .WithTrack("6:00", new TimeSpan(00, 05, 31))
            .WithTrack("Caught in a Web", new TimeSpan(00, 05, 28))
            .WithTrack("Innocence Faded", new TimeSpan(00, 05, 34)),
        new CD("Second cd", "TestArtist")
            .WithTrack("TrackForSecond", new TimeSpan(00, 13, 37))
    };

这里是 XML 创建:

var xDoc = new XDocument(
    new XDeclaration("1.0", "utf-8", "yes"),
    new XElement("cds",
          from cd in cds
          orderby cd.Artist
          select new XElement("cd",
               new XAttribute("artist", cd.Artist),
               new XAttribute("name", cd.Title),
               from t in cd.Tracks
               select new XElement("track",
                     new XElement("artist", t.Artist),
                     new XElement("title", t.Title),
                     new XElement("length", t.Length)));

您在这里遇到了几个问题 - 缺少根节点,并且在每次迭代中枚举所有 CD。

【讨论】:

    【解决方案2】:

    您的 XDocument 构造存在一些问题。

    1. XDocument 中必须只有一个根元素。您的陈述正在为每张 CD 构建一个根元素。
    2. 您的 LINQ 中有奇怪的嵌套循环。首先,您按艺术家订购 CD,然后在生成艺术家和名称属性时再次迭代整个 CD 收藏。您想从“当前”CD 中生成这些属性。
    3. 您在 LINQ 中使用“c1”和“t1”,而不是迭代变量“cl”和“t”。

    试试这个(请原谅我把你的 getter/setter 变成了属性:

    var xmlOutput = new XDocument(
        new XDeclaration("1.0", "utf-8", "yes"),
        new XElement(
            "cds",
            from cd in cds
            orderby cd.Artist.ToUpperInvariant()
            select new XElement(
                "cd",
                new XAttribute("title", cd.Title),
                new XAttribute("artist", cd.Artist),
                new XElement(
                    "tracks",
                    from t in cd.Tracks
                    select new XElement(
                        "track",
                        new XAttribute("artist", t.Artist),
                        new XAttribute("title", t.Title),
                        new XAttribute("length", t.Length))))));
    

    【讨论】:

      【解决方案3】:
      select new XElement("cd",  /*From new to the end of this is the error*/
              (
                 from c in cds
                     select new XAttribute("artist",c.getArtist())
              ),
      

      这将创建一个名为 cd 的元素(这很好),然后尝试为集合中的每张 CD 添加一个 artist 属性,这几乎肯定不是您想要的,并且是问题的原因。

      也就是说,这段代码试图让xml像这样:

      <cd
          artist="Dream Theater"
          artist="TestArtist"
      // the later stuff
      

      你可能知道这是非法的 xml。

      您缺少的想法是:

       (from cl in cds orderby cl.getArtist()
      

      您正在使用 LINQ 为您执行循环 - 在此 from 的范围内,c1收藏中的一张特定 CD。所以你不需要做from c in cds其中,因为你已经得到了你需要的CD对象:

          select new XElement("cd",  /*From new to the end of this is the error*/
              select new XAttribute("artist",c1.getArtist()),
              select new XAttribute("name", c1.getTitle()),
              new XElement("tracks",
                 (
                     from t in c1.getTracks()
                         select new XElement("track",
                             new XElement("artist",t1.getArtist()),    
                             new XElement("title",t1.getTitle()),
                             new XElement("length",t1.getLength())
                         )          
                 )                    
              )
          )
      )  
      

      你已经有了正确的选择c1.getTracks();在创建属性时应用相同的想法。

      【讨论】:

        猜你喜欢
        • 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
        相关资源
        最近更新 更多