using System.Xml.Linq;
// set the path to our working XML file string fileName = "base.xml"; // counter for composition number int trackId = 1; // Create nested constructors. XDocument doc = new XDocument ( new XElement ("library", new XElement ("track", new XAttribute ("id", trackId ++), new XAttribute ("genre", "Rap"), new XAttribute ("time", "3:24"), new XElement ("name", "Who We Be RMX (feat. 2Pac)"), new XElement ("artist", "DMX"), new XElement ("album", "The Dogz Mixtape: Who's Next ?!")), new XElement ("track", new XAttribute ("id", trackId ++), new XAttribute ("genre", "Rap"), new XAttribute ("time", "5:06"), new XElement ("name", "Angel (ft. Regina Bell)"), new XElement ("artist", "DMX"), new XElement ("album", "... And Then There Was X")), new XElement ("track", new XAttribute ("id", trackId ++), new XAttribute ("genre", "Break Beat"), new XAttribute ("time", "6:16"), new XElement ("name", "Dreaming Your Dreams"), new XElement ("artist", "Hybrid"), new XElement ("album", "Wide Angle")), new XElement ("track", new XAttribute ("id", trackId ++), new XAttribute ("genre", "Break Beat"), new XAttribute ("time", "9:38"), new XElement ("name", "Finished Symphony"), new XElement ("artist", "Hybrid"), new XElement ("album", "Wide Angle")))); // save our document doc.Save (fileName);
<? xml version = "1.0" encoding = "utf-8"?> <library> <track id = "1" genre = "Rap" time = "3:24"> <name> Who We Be RMX (feat. 2Pac) </ name> <artist> DMX </ artist> <album> The Dogz Mixtape: Who's Next?! </ album> </ track> <track id = "2" genre = "Rap" time = "5:06"> <Angel> Name (ft. Regina Bell) </ name> <artist> DMX </ artist> <album> ... And Then There Was X </ album> </ track> <track id = "3" genre = "Break Beat" time = "6:16"> <name> Dreaming Your Dreams </ name> <artist> Hybrid </ artist> <album> Wide Angle </ album> </ track> <track id = "4" genre = "Break Beat" time = "9:38"> <name> Finished Symphony </ name> <artist> Hybrid </ artist> <album> Wide Angle </ album> </ track> </ library>
XDocument doc = new XDocument (); XElement library = new XElement ("library"); doc.Add (library); // create the element "track" XElement track = new XElement ("track"); // add necessary attributes track.Add (new XAttribute ("id", 1)); track.Add (new XAttribute ("genre", "Rap")); track.Add (new XAttribute ("time", "3:24")); // create the element "name" XElement name = new XElement ("name"); name.Value = "Who We Be RMX (feat. 2Pac)"; track.Add (name); // create the element "artist" XElement artist = new XElement ("artist"); artist.Value = "DMX"; track.Add (artist); // For variety, parse element "album" string albumData = "<album> The Dogz Mixtape: Who's Next?! </ album>"; XElement album = XElement.Parse (albumData); track.Add (album); doc.Root.Add (track); / * * add other elements by analogy * / // save our document doc.Save (fileName);
// set the path to our working XML file string fileName = "base.xml"; // read data from file XDocument doc = XDocument.Load (fileName); // we pass on each element in the library // (this element is immediately available through the doc.Root property) foreach (XElement el in doc.Root.Elements ()) { // Display the name of the element and the value of the attribute id Console.WriteLine ("{0} {1}", el.Name, el.Attribute ("id"). Value); Console.WriteLine ("Attributes:"); // we loop out all the attributes, at the same time we see how they convert themselves into a string foreach (XAttribute attr in el.Attributes ()) Console.WriteLine ("{0}", attr); Console.WriteLine ("Elements:"); // in the loop we print the names of all the child elements and their values foreach (XElement element in el.Elements ()) Console.WriteLine ("{0}: {1}", element.Name, element.Value); }
track 1 Attributes: id = "1" genre = "Rap" time = "3:24" Elements: name: Who We Be RMX (feat. 2Pac) artist: DMX album: The Dogz Mixtape: Who's Next ?! track 2 Attributes: id = "2" genre = "Rap" time = "5:06" Elements: name: Angel (ft. Regina Bell) artist: DMX album: ... And Then There Was X track 3 Attributes: id = "3" genre = "Break Beat" time = "6:16" Elements: name: Dreaming Your Dreams artist: Hybrid album: Wide Angle track 4 Attributes: id = "4" genre = "Break Beat" time = "9:38" Elements: name: Finished Symphony artist: Hybrid album: Wide Angle
// Get the first child node from the library XNode node = doc.Root.FirstNode; while (node! = null) { // check that the current node is an element if (node.NodeType == System.Xml.XmlNodeType.Element) { XElement el = (XElement) node; // get the value of the id attribute and convert it to Int32 int id = Int32.Parse (el.Attribute ("id"). Value); // increment the counter by one and set the value back id ++; el.Attribute ("id"). Value = id.ToString (); } // go to the next node node = node.NextNode; } doc.Save (fileName);
foreach (XElement el in doc.Root.Elements ("track")) { int id = Int32.Parse (el.Attribute ("id"). Value); el.SetAttributeValue ("id", - id); } doc.Save (fileName);
int maxId = doc.Root.Elements ("track"). Max (t => Int32.Parse (t.Attribute ("id"). Value)); XElement track = new XElement ("track", new XAttribute ("id", ++ maxId), new XAttribute ("genre", "Break Beat"), new XAttribute ("time", "5:35"), new XElement ("name", "Higher Than A Skyscraper"), new XElement ("artist", "Hybrid"), new XElement ("album", "Morning Sci-Fi")); doc.Root.Add (track); doc.Save (fileName);
IEnumerable <XElement> tracks = doc.Root.Descendants ("track"). Where ( t => t.Element ("artist"). Value == "DMX"). ToList (); foreach (XElement t in tracks) t.Remove ();
IEnumerable <XElement> tracks = doc.Root.Descendants ("track"). Where ( t => t.Element ("artist"). Value == "DMX"); tracks.Remove ();
IEnumerable <XElement> tracks = from t in doc. Root. Elements ("track") let time = DateTime.Parse (t.Attribute ("time"). Value) orderby time descending select t; foreach (XElement t in tracks) Console.WriteLine ("{0} - {1}", t.Attribute ("time"). Value, t.Element ("name"). Value);
IEnumerable <XElement> tracks = from t in doc. Root. Elements ("track") orderby t.Attribute ("genre"). Value, t.Element ("artist"). Value, t.Element ("album"). Value, t.Element ("name"). Value select t; foreach (XElement t in tracks) { Console.WriteLine ("{0} - {1} - {2} - {3}", t.Attribute ("genre"). Value, t.Element ("artist"). Value, t.Element ("album"). Value, t.Element ("name"). Value); }
var albumGroups = doc.Root.Elements ("track"). GroupBy (t => t.Element ("album"). Value); foreach (IGrouping <string, XElement> a in albumGroups) Console.WriteLine ("{0} - {1}", a.Key, a.Count ());
Source: https://habr.com/ru/post/24673/
All Articles