public ElementAttributeMatch(ElementMatch elementMatch, AttributeMatch attributeMatch) { Guard.ArgumentNotNull(elementMatch, "elementMatch"); Guard.ArgumentNotNull(attributeMatch, "attributeMatch"); this.elementMatch = elementMatch; this.attributeMatch = attributeMatch; }
public static List <XmlMatch> Parse(string expression, MatchMode mode) { Guard.ArgumentNotNullOrEmptyString(expression, "expression"); bool isRoot = false; List <XmlMatch> names = new List <XmlMatch>(); string normalized = expression; if (normalized.StartsWith("//", StringComparison.Ordinal)) { normalized = normalized.Substring(2); } else if (normalized.StartsWith("/", StringComparison.Ordinal)) { isRoot = true; normalized = normalized.Substring(1); } string[] paths = normalized.Split('/'); try { for (int i = 0; i < paths.Length; i++) { string path = paths[i]; if (path.Length == 0) { ThrowInvalidPath(expression); } // Attribute match can only be the last in the expression. if (path.StartsWith("@", StringComparison.Ordinal) && i != paths.Length - 1) { throw new ArgumentException(String.Format( CultureInfo.CurrentCulture, Properties.Resources.AttributeAxisInvalid, expression)); } XmlMatch match; string[] xmlName = path.Split(':'); string prefix, name; if (xmlName.Length == 2) { prefix = xmlName[0]; name = xmlName[1]; if (prefix.Length == 0) { ThrowInvalidPath(expression); } else if (name.Length == 0) { ThrowInvalidPath(expression); } } else { name = xmlName[0]; prefix = String.Empty; } match = CreateMatch( isRoot && i == 0, /* Only pass the actual match mode when we're at the last segment */ (i == paths.Length - 1) ? mode : MatchMode.StartElement, prefix, name); if (match is AttributeMatch && names.Count > 0) { // Build a composite that matches element with the given attribute. ElementMatch parent = names[names.Count - 1] as ElementMatch; if (mode == MatchMode.EndElement) { throw new ArgumentException(Properties.Resources.AttributeMatchCannotBeEndElement); } names.RemoveAt(names.Count - 1); names.Add(new ElementAttributeMatch(parent, (AttributeMatch)match)); } else { names.Add(match); } } } catch (ArgumentException aex) { throw new ArgumentException(String.Format( CultureInfo.CurrentCulture, Properties.Resources.InvalidPath, expression), aex); } return(names); }