现在我要开始说这确实是一个任务。然而,在遇到 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 的对象的原因。 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!
),
如果您能提供任何帮助,我们将非常高兴!
最佳答案
首先,我建议您对方法使用属性和 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 class - 私有(private) setter 不允许更改此类之外的属性值。这是轨道类:
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。
关于c# - LINQ to XML - 如何正确使用 XDocument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16795396/