// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Linq; using System.Xml; using System.Xml.Linq; namespace E { public class Program { public static void Main(string[] args) { // XmLDocument XmlDocument doc = new XmlDocument(); doc.Load(@"C:\work\rdf.xml"); XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable); mgr.AddNamespace("x", "adobe:ns:meta/"); mgr.AddNamespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); mgr.AddNamespace("dc", "http://purl.org/dc/elements/1.1/"); foreach(XmlNode n in doc.SelectNodes("/x:xmpmeta/rdf:RDF/rdf:Description/dc:subject/rdf:Bag/rdf:li/text()", mgr)) { Console.WriteLine(n.Value); } foreach(XmlNode n in doc.GetElementsByTagName("rdf:li")) { Console.WriteLine(n.FirstChild.Value); } // XDocument XDocument xd = XDocument.Load(@"C:\work\rdf.xml"); XNamespace x = "adobe:ns:meta/"; XNamespace rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"; XNamespace dc = "http://purl.org/dc/elements/1.1/"; foreach(XText n in xd.Element(x + "xmpmeta").Elements(rdf + "RDF").Elements(rdf + "Description").Elements(dc + "subject").Elements(rdf + "Bag").Elements(rdf + "li").DescendantNodes()) { Console.WriteLine(n.Value); } foreach(XText n in xd.Descendants(rdf + "li").DescendantNodes()) { Console.WriteLine(n.Value); } } } }