private static string ConvertCollectionToString(ICollection <XName> names) { StringBuilder builder = new StringBuilder(); foreach (var name in names) { var ns = SrcMLNamespaces.LookupPrefix(name.Namespace.NamespaceName); builder.AppendFormat(", {0}:{1}", ns, name.LocalName); } builder.Remove(0, 2); return(builder.ToString()); }
/// <summary> /// Gets an XPath query that uniquely identifies the given XElement /// </summary> /// <param name="element">The element to create an XPath query for</param> /// <param name="relativeToFileUnit">whether or not the XPath query is relative to the parent file unit or not</param> /// <returns>An XPath query that uniquely identifies <paramref name="element"/></returns> public static string GetXPath(this XElement element, bool relativeToFileUnit) { if (null == element) { throw new ArgumentNullException("element"); } StringBuilder xpathBuilder = new StringBuilder(); XElement current = element; do { if (SRC.Unit == current.Name) { if (relativeToFileUnit) { current = null; continue; } var fileName = current.Attribute("filename"); if (fileName != null) { xpathBuilder.Insert(0, String.Format(CultureInfo.InvariantCulture, "[@filename=\"{0}\"]", fileName.Value)); } } else { var count = current.ElementsBeforeSelf(current.Name).Count() + 1; xpathBuilder.Insert(0, String.Format(CultureInfo.InvariantCulture, "[{0}]", count)); } var prefix = SrcMLNamespaces.LookupPrefix(current.Name.NamespaceName); xpathBuilder.Insert(0, String.Format(CultureInfo.InvariantCulture, "{0}", current.Name.LocalName)); if (!String.IsNullOrEmpty(prefix)) { xpathBuilder.Insert(0, String.Format(CultureInfo.InvariantCulture, "{0}:", prefix)); } xpathBuilder.Insert(0, "/"); current = (null == current.Parent ? null : current.Parent); } while(null != current); return(xpathBuilder.ToString()); }