SelectAncestors() public method

public SelectAncestors ( XPathNodeType type, bool matchSelf ) : XPathNodeIterator
type XPathNodeType
matchSelf bool
return XPathNodeIterator
 private void AddSchemaElement(XPathNavigator nav, XPathNavigator rootNav)
 {
     List<string> list = new List<string>();
     XPathNodeIterator iterator = nav.SelectAncestors(XPathNodeType.Element, true);
     while (iterator.MoveNext())
     {
         list.Add(iterator.Current.Name);
         if (iterator.Current.IsSamePosition(rootNav))
         {
             break;
         }
     }
     list.Reverse();
     OrderedDictionary first = this._rootSchema;
     Pair pair = null;
     foreach (string str in list)
     {
         pair = first[str] as Pair;
         if (pair == null)
         {
             pair = new Pair(new OrderedDictionary(), new ArrayList());
             first.Add(str, pair);
         }
         first = (OrderedDictionary) pair.First;
     }
     this.AddAttributeList(nav, (ArrayList) pair.Second);
 }
Example #2
0
            public NodeInfo(XPathNavigator current)
            {
                List<string> xpathParts = new List<string>();
                XPathNodeIterator selection = current.SelectAncestors(XPathNodeType.All, true);

                while (selection.MoveNext())
                {
                    if (selection.Current.NodeType == XPathNodeType.Root)
                        break;

                    var node = selection.Current;
                    var name = node.Name;

                    if (!string.IsNullOrEmpty(node.NamespaceURI))
                    {
                        string prefix = this.GetPrefix(node);
                        name = string.Concat(prefix, ":", node.LocalName);
                    }

                    xpathParts.Add(
                        node.NodeType == XPathNodeType.Attribute
                            ? string.Format(AttributeXPath, name)
                            : string.Format(ElementXPath, name, this.GetNodeIndex(node)));

                    this.Root = node;
                }

                xpathParts.Reverse();

                this.XPath = string.Join("/", xpathParts);
            }
Example #3
0
		static void WriteElement( StreamWriter writer, int indent, XPathNavigator element ) {
			string elementPrefix = new string( ' ', indent * 2 );
			string attributePrefix = new string( ' ', indent * 2 + 2 );

			writer.Write( "{0}<{1}", elementPrefix, element.Name );

			foreach ( XPathNavigator attribute in element.Select( "@*" ) ) {
				if ( element.Name == "VisualStudioProject" && attribute.Name == "Version" ) {
					writer.Write( "{0}{1}Version=\"8.00\"", Environment.NewLine, attributePrefix );
				}
				else if ( element.Name == "VisualStudioProject" && attribute.Name == "TargetFrameworkVersion" ) {
					// No op.
				}
				else if ( element.Name == "AssemblyReference" && attribute.Name == "MinFrameworkVersion" ) {
					// No op.
				}
				else if ( element.Name == "ProjectReference" && attribute.Name == "Include" ) {
					string newValue = Path.GetFileNameWithoutExtension( attribute.Value );
					newValue += ".2005" + Path.GetExtension( attribute.Value );
					writer.Write( " Include=\"{0}\"", newValue );
				}
				else if ( element.Name == "Import" && attribute.Name == "Project" ) {
					string newValue = attribute.Value.Replace( "$(MSBuildToolsPath)", "$(MSBuildBinPath)" );
					writer.Write( " Project=\"{0}\"", newValue );
				}
				else {
					writer.Write( "{0}{1}{2}=\"{3}\"", Environment.NewLine, attributePrefix, attribute.Name, attribute.Value );
				}
			}

			if ( element.Name == "Project" )
			{
				XPathNodeIterator ancestors = element.SelectAncestors(XPathNodeType.Element, false);
				if ( ancestors.Count == 0 )
					writer.Write( Environment.NewLine + "  xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\"" );
			}

			if ( element.HasChildren ) {
				writer.Write( ">" );

				bool writeNewline = true;
				foreach ( XPathNavigator child in element.SelectChildren( XPathNodeType.Element ) ) {
					if ( writeNewline )
					{
						writer.WriteLine();
						writeNewline = false;
					}

					WriteElement( writer, indent + 1, child );
				}

				foreach ( XPathNavigator child in element.SelectChildren( XPathNodeType.Text ) ) {
					writer.Write( child.Value );
				}

				if ( !writeNewline )
				{
					writer.Write(elementPrefix);
				}

				writer.WriteLine( "</{0}>", element.Name );
			}
			else {
				writer.WriteLine( "/>" );
			}
		}
Example #4
0
        private bool CheckOr(XPathNavigator rowNavigator)
        {
            if (orIDs.Count == 0)
            {
                return true;
            }

            bool check = false;

            XPathNodeIterator ancestors = rowNavigator.SelectAncestors(XPathNodeType.Element, true);

            foreach (XPathNavigator ancestor in ancestors)
            {
                String ancestorIDString = ancestor.GetAttribute(idAttribute, ancestor.NamespaceURI);

                if (ancestor.Name.Equals("TimelineRow") && ancestorIDString != null && ancestorIDString.Length > 0)
                {
                    int intID = Int32.Parse(ancestorIDString);

                    if (orIDs.Contains(intID))
                    {
                        check = true;
                    }
                }
            }

            XPathNodeIterator descendants = rowNavigator.Select(".//TimelineRow | .//TimelineFilter");
            foreach (XPathNavigator descendant in descendants)
            {
                String descendantIDString = descendant.GetAttribute(idAttribute, descendant.NamespaceURI);

                if (descendantIDString != null && descendantIDString.Length > 0)
                {
                    int intID = Int32.Parse(descendantIDString);

                    if (orIDs.Contains(intID))
                    {
                        check = true;
                    }
                }
            }
            
            return check;
        }
        private string CreateLocation(XPathNavigator context)
        {
            Stack<string> steps = new Stack<string>();

            if (context.NodeType == XPathNodeType.Attribute)
            {
                steps.Push(String.Format("@{0}", context.Name));
            }
            else if (context.NodeType == XPathNodeType.Text)
            {
                steps.Push("text()");
            }

            XPathNodeIterator ancestors = context.SelectAncestors(XPathNodeType.Element, true);
            while (ancestors.MoveNext())
            {
                XPathNavigator current = ancestors.Current;
                if (current.NodeType == XPathNodeType.Element)
                {
                    // resolve namespace
                    XmlNamespaceManager nsManager = new XmlNamespaceManager(new NameTable());
                    nsManager.AddNamespace(current.Prefix, current.NamespaceURI);

                    // resolve name + position
                    string name = current.Name;
                    int position = 1 + ancestors.Current.Select(String.Format("preceding-sibling::{0}", name), nsManager).Count;
                    steps.Push(String.Format("{0}[{1}]", name, position));
                }
            }

            // results
            StringBuilder sb = new StringBuilder();
            while (steps.Count > 0)
            {
                sb.Append("/");
                sb.Append(steps.Pop());
            }

            return sb.ToString();
        }