public bool Accept(XQueryNavigator nav) { return targets == null || targets.Contains(nav.DmNode); }
/// <summary> /// Runs the solutions to all problems /// </summary> public static void Main() { XmlDocument doc = new XmlDocument(); doc.Load(PathToXmlFile); XmlNode root = doc.DocumentElement ?? doc.CreateElement("root"); // Problem 2: Write program that extracts all different artists which are found in the catalog.xml. // For each author you should print the number of albums in the catalogue. // Use the DOM parser and a hash - table. PrintNumbersOfAlbumsForEachArtist(root); // Problem 3: Implement the previous using XPath. PrintArtistsNumberOfAlbumsUsingXPath(root); // Problem 4: Using the DOM parser write a program to delete from catalog.xml all albums having price > 20. DeleteAlbumsByPrice(root, 20.0); // Check that albums are deleted: PrintNumbersOfAlbumsForEachArtist(root); // Problem 5: Write a program, which using XmlReader extracts all song titles from catalog.xml. var songTitles = ExtractSongTitlesFromCatalogue(PathToXmlFile); Console.WriteLine("Song titles: " + string.Join(", ", (songTitles as List <string>).ToArray())); // Problem 6: Rewrite the same using XDocument and LINQ query. XDocument xDoc = XDocument.Load(PathToXmlFile); var songTitlesUsingLinq = from songs in xDoc.Descendants("title") select songs.Value.Trim(); Console.WriteLine("Song titles (using LINQ): " + string.Join(", ", songTitlesUsingLinq)); // Problem 7: In a text file we are given the name, address and phone number of given person (each at a single line). // Write a program, which creates new XML document, which contains these data in structured XML format. CreateXmlPhonebook("../../phonebook.txt"); // Problem 8: Write a program, which (using XmlReader and XmlWriter) reads the file catalog.xml and creates the file album.xml, // in which stores in appropriate way the names of all albums and their authors. CreateAlbumsXml(PathToXmlFile); // Problem 9: Write a program to traverse given directory and write to a XML file its contents together with all subdirectories and files. // Use tags < file > and < dir > with appropriate attributes. // For the generation of the XML document use the class XmlWriter. using (var writer = new XmlTextWriter("../../traverseWithXmlWriter.xml", Encoding.UTF8)) { writer.WriteStartDocument(); writer.WriteStartElement("DirectoriesRoot"); CreateFileSystemXmlTreeUsingXmlWriter("../../..", writer); writer.WriteEndElement(); writer.WriteEndDocument(); writer.Close(); } // Problem 10: Rewrite the last exercises using XDocument, XElement and XAttribute. var xDocument = new XDocument(); xDocument.Add(CreateFileSystemXmlTree("../../../")); xDocument.Save("../../traverseWithXElement.xml"); // Problem 11: Write a program, which extract from the file catalog.xml the prices for all albums, published 5 years ago or earlier. // Use XPath query. doc.Load(PathToXmlFile); root = doc.DocumentElement; // returns all (no albums in the catalogue are newer...) var oldAlbumsPrices = root.SelectNodes("album/price[../year/text() < 2010]"); //// var oldAlbumsPrices = root.SelectNodes("album/price[../year/text() < 1980]"); // returns 2 albums' prices Console.WriteLine(new string('-', 50)); Console.WriteLine("Prices of the albums, published before 2010: "); foreach (var price in oldAlbumsPrices) { Console.WriteLine((price as XmlElement).InnerXml.Trim()); } // Problem 12: Rewrite the previous using LINQ query. Console.WriteLine(new string('-', 50)); Console.WriteLine("Prices of the albums, published before 2010 (using LINQ): "); var oldAlbumsPricesUsingLinq = from album in xDoc.Descendants("album") where int.Parse(album.Element("year").Value) < 2010 select album.Descendants("price").FirstOrDefault(); foreach (var price in oldAlbumsPricesUsingLinq) { Console.WriteLine(price.Value.Trim()); } // Problem 13: Create an XSL stylesheet, which transforms the file catalog.xml into HTML document, // formatted for viewing in a standard Web-browser. // Problem 14: Write a C# program to apply the XSLT stylesheet transformation on the file catalog.xml // using the class XslTransform. XslCompiledTransform catalogueXslt = new XslCompiledTransform(); catalogueXslt.Load("../../catalogue.xslt"); catalogueXslt.Transform(PathToXmlFile, "../../catalogue.html"); // Problem 15: // *Read some tutorial about the XQuery language. // Implement the XML to HTML transformation with XQuery (instead of XSLT). // Download some open source XQuery library for .NET and execute the XQuery to transform the catalog.xml to HTML. XQueryNavigatorCollection col = new XQueryNavigatorCollection(); // Add the XML document catalogue.xml to the collection using cat as the name to reference. col.AddNavigator("../../catalogue.xml", "cat"); var expr = new XQueryExpression( "<html><body><head><title>Catalogue</title></head>" + "<h1>Catalogue generated using XQuery</h1>" + "{For $a IN document(\"cat\")/catalogue/album " + "RETURN <div><strong>Title:</strong> {$a/name/text()}<br />" + "<strong>Artist:</strong> {$a/artist/text()}<br />" + "<strong>Year:</strong> {$a/year/text()}<br />" + "<strong>Producer:</strong> {$a/producer/text()}<br />" + "<strong>Price:</strong> {$a/price/text()}<br />" + "<strong>Songs:</strong><ol>{For $s IN $a/songs/song RETURN <li>{$s/title/text()}</li>}</ol>" + "</div><hr />}</body></html>"); StreamWriter str = new StreamWriter("../../catalogueUsingXQuery.html"); XQueryNavigator nav = expr.Execute(col); nav.ToXml(str); str.Close(); // Problem 16: // Using Visual Studio generate an XSD schema for the file catalog.xml. // Write a C# program that takes an XML file and an XSD file (schema) and validates the XML file against the schema. // Test it with valid XML catalogs and invalid XML catalogs. string xsdMarkup = File.ReadAllText("../../catalogue.xsd"); XmlSchemaSet schemas = new XmlSchemaSet(); schemas.Add(string.Empty, XmlReader.Create(new StringReader(xsdMarkup))); XDocument valid = XDocument.Load(PathToXmlFile); XDocument invalid = new XDocument( new XElement( "Root", new XElement("Child1", "content1"), new XElement("Child2", "content2"))); Console.WriteLine(new string('-', 50)); Console.WriteLine("Validating valid document:"); bool errors = false; valid.Validate(schemas, (o, e) => { Console.WriteLine("{0}", e.Message); errors = true; }); Console.WriteLine("Valid {0}", errors ? "did not validate" : "validated"); Console.WriteLine(); Console.WriteLine("Validating invalid document:"); errors = false; invalid.Validate(schemas, (o, e) => { Console.WriteLine("{0}", e.Message); errors = true; }); Console.WriteLine("doc2 {0}", errors ? "did not validate" : "validated"); }
private IEnumerable<XPathItem> Iterator(XQueryNavigator src) { int inf; int sup; int length = src.GetLength(); DmNode node = src.DmNode; NodeSet nset; lock (node) { nset = node.GetNodeSet(this); if (nset == null) nset = node.CreateNodeSet(this, _path); nset.GetBounds(out inf, out sup); } int index; if (src.Position < inf) { index = inf; length -= inf - src.Position; } else index = src.Position; length = Math.Min(length, sup - inf + 1); XQueryNavigator res = (XQueryNavigator)src.Clone(); PageFile pf = src.Document.pagefile; int[] buffer = new int[XQueryLimits.DirectAcessBufferSize]; while (length > 0) { int size = pf.Select(nset.hindex, ref index, ref length, buffer); for (int k = 0; k < size; k++) { res.Position = buffer[k]; if (nset.Accept(res)) yield return res; if (nset.mixed && res.IsLeafTextElement) { res.MoveToFirstChild(); if (nset.Accept(res)) yield return res; } } } }
public XPathItem this[int i] { get { if (i < pos) { current = null; pos = 0; } if (current == null) { index = 0; offset = 0; if (owner.segments.Count == 0) return null; current = owner.segments[0]; nav = null; } while (pos < i) { if (offset == current.Count - 1) { index++; if (index == owner.segments.Count) return null; current = owner.segments[index]; offset = 0; nav = null; } else offset++; pos++; } object packedItem = current[offset]; XPathItem item = packedItem as XPathItem; if (item != null) return item; if (nav == null) nav = current.document.CreateNavigator(); nav.Position = (int)packedItem; return nav; } }