/// <summary> /// Determine if current reader position matches given node pattern /// </summary> /// <param name="pattern">Definition of a single node</param> /// <returns>True if current node matches pattern; false if not.</returns> private bool MatchFound(XmlNodeDef pattern) { if (_xmlReader.NodeType != XmlNodeType.Element) { return(false); } if (_xmlReader.Name != pattern.Name) { return(false); } //Name matches, let's look for attributes: var attribsToMatch = pattern.GetAttributeDict(); if (!attribsToMatch.Any()) { return(true); } var retVal = false; if (_xmlReader.HasAttributes) { //locate attributes matching name and value while (_xmlReader.MoveToNextAttribute()) { if (attribsToMatch.ContainsKey(_xmlReader.Name)) { //attribute name match, remove from the list to match, but only if value matches (or no value in pattern) var valueToMatch = attribsToMatch[_xmlReader.Name]; if (valueToMatch == null || valueToMatch.ToString() == _xmlReader.Value) { attribsToMatch.Remove(_xmlReader.Name); if (!attribsToMatch.Any()) { //success! we matched all attributes retVal = true; break; } } else { break; //match failed; no chance to run into the same attribute again } } } } return(retVal); }
public void GetAttributeDict_ComplexSpecs_CorrectData() { //arrange var specs = "MyName[@id][@key1=x][@key2=\"\"]"; //act var actual = new XmlNodeDef(specs); var attribDict = actual.GetAttributeDict(); //assert actual.Name.Should().Be("MyName"); attribDict.Should().BeOfType <Dictionary <string, object> >(); attribDict.Count.Should().Be(3); attribDict["id"].Should().BeNull(); attribDict["key1"].Should().Be("x"); attribDict["key2"].Should().Be(string.Empty); }