CSharp · Console
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
using System.Diagnostics;
using System.Collections.ObjectModel;
using System.Threading;
using System.ComponentModel;
using System.Xml.Linq;
using System.Reflection;
using System.Data;
using System.Xml.Schema;
using System.Text.RegularExpressions;
using System.Net;
using System.Xml.XPath;
using System.Globalization;
using System.ComponentModel.DataAnnotations;
using System.Collections;
public class Program
{
public static void Main()
{
var reader = new XmlDocument();
reader.Load(@"http://www.fieldgulls.com/rss/current");
var nsmgr = new XmlNamespaceManager(reader.NameTable);
nsmgr.AddNamespace("a", "http://www.w3.org/2005/Atom");
XmlNodeList list = reader.SelectNodes(".//a:entry", nsmgr);
Console.WriteLine("Nodes found: {0}", list.Count);
foreach (XmlElement element in list)
{
Console.WriteLine("Name: {0}", XName.Get(element.LocalName, element.NamespaceURI));
Console.WriteLine("Path without namespaces: {0}", element.GetXPath_SequentialIteration());
//Console.WriteLine(element.OuterXml);
}
Console.WriteLine("Dumping a list of all element names and namespaces: ");
Console.WriteLine(String.Join("\n", XDocument.Load(@"http://www.fieldgulls.com/rss/current").DumpXmlElementNames()));
}
}
public static class XObjectExtensions
{
public static IEnumerable<string> DumpXmlElementNames(this XDocument doc)
{
return doc.Root.DumpXmlElementNames();
}
public static IEnumerable<string> DumpXmlElementNames(this XElement root)
{
if (root == null)
return Enumerable.Empty<string>();
var startCount = root.AncestorsAndSelf().Count();
return root.DescendantsAndSelf().Select(el => string.Format("{0}\"{1}\"",
new string(' ', 2 * (el.AncestorsAndSelf().Count() - startCount)), el.Name.ToString()));
}
}
public static class XmlElementExtensions
{
// From http://stackoverflow.com/questions/9221073/get-full-path-to-current-node
public static string GetXPath_SequentialIteration(this XmlElement element)
{
string path = "/" + element.Name;
XmlElement parentElement = element.ParentNode as XmlElement;
if (parentElement != null)
{
// Gets the position within the parent element.
// However, this position is irrelevant if the element is unique under its parent:
XmlNodeList siblings = parentElement.SelectNodes(element.Name);
if (siblings != null && siblings.Count > 1) // There's more than 1 element with the same name
{
int position = 1;
foreach (XmlElement sibling in siblings)
{
if (sibling == element)
break;
position++;
}
path = path + "[" + position + "]";
}
// Climbing up to the parent elements:
path = parentElement.GetXPath_SequentialIteration() + path;
}
return path;
}
}