Example #1
0
        /// <summary>
        /// Gets the parent element path based on the index position. This
        /// method does not compact the path so it will include all elements
        /// including those in another namespace in the path.
        /// </summary>
        static XmlElementPath GetFullParentElementPath(string xml, QualifiedNameCollection namespaces)
        {
            XmlElementPath path = new XmlElementPath();
            IDictionary <string, string> namespacesInScope = null;

            using (StringReader reader = new StringReader(xml)) {
                using (XmlTextReader xmlReader = new XmlTextReader(reader)) {
                    try {
                        xmlReader.XmlResolver = null;                         // prevent XmlTextReader from loading external DTDs
                        while (xmlReader.Read())
                        {
                            switch (xmlReader.NodeType)
                            {
                            case XmlNodeType.Element:
                                if (!xmlReader.IsEmptyElement)
                                {
                                    QualifiedName elementName = new QualifiedName(xmlReader.LocalName, xmlReader.NamespaceURI, xmlReader.Prefix);
                                    path.Elements.Add(elementName);
                                }
                                break;

                            case XmlNodeType.EndElement:
                                path.Elements.RemoveLast();
                                break;
                            }
                        }
                    } catch (XmlException) {
                        namespacesInScope = xmlReader.GetNamespacesInScope(XmlNamespaceScope.All);
                    }
                }
            }

            // Add namespaces in scope for the last element read.
            if (namespacesInScope != null)
            {
                foreach (KeyValuePair <string, string> ns in namespacesInScope)
                {
                    namespaces.Add(new QualifiedName(String.Empty, ns.Value, ns.Key));
                }
            }

            return(path);
        }
 public void AddElement(QualifiedName elementName)
 {
     elements.Add(elementName);
 }
Example #3
0
		/// <summary>
		/// Gets the parent element path based on the index position. This
		/// method does not compact the path so it will include all elements
		/// including those in another namespace in the path.
		/// </summary>
		static XmlElementPath GetFullParentElementPath(string xml, QualifiedNameCollection namespaces)
		{
			XmlElementPath path = new XmlElementPath();
			IDictionary<string,string> namespacesInScope = null;
			using (StringReader reader = new StringReader(xml)) {
				using (XmlTextReader xmlReader = new XmlTextReader(reader)) {
					try {
						xmlReader.XmlResolver = null; // prevent XmlTextReader from loading external DTDs
						while (xmlReader.Read()) {
							switch (xmlReader.NodeType) {
								case XmlNodeType.Element:
									if (!xmlReader.IsEmptyElement) {
										QualifiedName elementName = new QualifiedName(xmlReader.LocalName, xmlReader.NamespaceURI, xmlReader.Prefix);
										path.Elements.Add(elementName);
									}
									break;
								case XmlNodeType.EndElement:
									path.Elements.RemoveLast();
									break;
							}
						}
					} catch (XmlException) {
						namespacesInScope = xmlReader.GetNamespacesInScope(XmlNamespaceScope.All);
					}
				}
			}
			
			// Add namespaces in scope for the last element read.
			if (namespacesInScope != null) {
				foreach (KeyValuePair<string, string> ns in namespacesInScope) {
					namespaces.Add(new QualifiedName(String.Empty, ns.Value, ns.Key));
				}
			}
			
			return path;
		}