Now I will start by saying that this is really an assistant. However, I almost finished it until I came across the Linq to XML syntax.
I have 2 classes: the track and the CD are now part of the set, I create a CD, and then add several tracks to it. After searching for a large number of tutorials that perfectly explained how to switch from xml to objects, I just can't get this to work (objects in xml).
I currently have:
//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);
Now this is what gets me the objects that I need to put in XML. XML part:
XDocument xmlOutput = new XDocument ( new XDeclaration("1.0","utf-8","yes"), (from cl in cds orderby cl.getArtist() select new XElement("cd", ( 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);
This works great (gets me the result I need!) In just 1 CD. When I decide to add another CD, it shows:
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Xml.Linq.dll Duplicate Attribute (cd)
Which points to XDocument. Other than that it doesn't work, it feels pretty stupid (from c to cds x2), but all I try, I canβt stop this syntax from hating me:
( from c in cds select new XAttribute("artist",c.getArtist()), select new XAttribute("name", c.getTitle()) //No not happening! ),
I would be very happy for any help you can provide!