GetNamespacesInScope() public method

public GetNamespacesInScope ( XmlNamespaceScope scope ) : string>.IDictionary
scope XmlNamespaceScope
return string>.IDictionary
        public string GetValue(XElement element, XmlNamespaceManager nsm)
        {
            XPathContext context = new XPathContext((NameTable)nsm.NameTable);
            XPathNavigator navigator = element.CreateNavigator();
            object result = null;

            foreach (var ns in nsm.GetNamespacesInScope(XmlNamespaceScope.All))
                context.AddNamespace(ns.Key, ns.Value);

            context.Arguments.AddParam(XPathContext.ParameterNames.CurrentNode, string.Empty, navigator.Select("."));
            result = navigator.Evaluate(this.RawValue, context);

            if (result is string)
                return (string)result;
            else if (result is XPathNodeIterator)
            {
                var iterator = ((XPathNodeIterator)result);
                var current = (XPathNavigator)((IEnumerable)iterator).Cast<object>().First();

                return current.Value;
            }
            else if (result is XAttribute)
                return ((XAttribute)result).Value;
            else if (result is XElement)
                return ((XElement)result).Value;

            return string.Empty;
        }
示例#2
0
//
// IXmlNamespaceResolver implementation
//
        IDictionary <string, string> IXmlNamespaceResolver.GetNamespacesInScope(XmlNamespaceScope scope)
        {
            if (!InNamespaceActiveState)
            {
                return(new Dictionary <string, string>());
            }
            return(nsManager.GetNamespacesInScope(scope));
        }
        public XmlNode Strip(XmlNode documentElement)
        {
            var namespaceManager = new XmlNamespaceManager(documentElement.OwnerDocument.NameTable);
            foreach (var nspace in namespaceManager.GetNamespacesInScope(XmlNamespaceScope.All))
            {
                namespaceManager.RemoveNamespace(nspace.Key, nspace.Value);
            }

            return documentElement;
        }
示例#4
0
        public IDictionary <string, string> GetNamespacesInScope(XmlNamespaceScope scope)
        {
            IDictionary <string, string> dictionary = _nsManager.GetNamespacesInScope(scope);

            if (scope != XmlNamespaceScope.Local)
            {
                XmlNode node = _startNode;
                while (node != null)
                {
                    switch (node.NodeType)
                    {
                    case XmlNodeType.Element:
                        XmlElement elem = (XmlElement)node;
                        if (elem.HasAttributes)
                        {
                            XmlAttributeCollection attrs = elem.Attributes;
                            for (int i = 0; i < attrs.Count; i++)
                            {
                                XmlAttribute attr = attrs[i];
                                if (Ref.Equal(attr.NamespaceURI, _document.strReservedXmlns))
                                {
                                    if (attr.Prefix.Length == 0)
                                    {
                                        // xmlns='' declaration
                                        if (!dictionary.ContainsKey(string.Empty))
                                        {
                                            dictionary.Add(string.Empty, attr.Value);
                                        }
                                    }
                                    else
                                    {
                                        // xmlns:prefix='' declaration
                                        if (!dictionary.ContainsKey(attr.LocalName))
                                        {
                                            dictionary.Add(attr.LocalName, attr.Value);
                                        }
                                    }
                                }
                            }
                        }
                        node = node.ParentNode;
                        break;

                    case XmlNodeType.Attribute:
                        node = ((XmlAttribute)node).OwnerElement;
                        break;

                    default:
                        node = node.ParentNode;
                        break;
                    }
                }
            }
            return(dictionary);
        }
示例#5
0
 IDictionary <string, string> IXmlNamespaceResolver.GetNamespacesInScope(XmlNamespaceScope scope)
 {
     return(_namespaceManager.GetNamespacesInScope(scope));
 }
        public FeatureCollectionResult ImportResults(IOpenSearchResultCollection results)
        {
            if (results == null)
                throw new ArgumentNullException("results");

            FeatureCollectionResult fc = new FeatureCollectionResult();
            fc.Properties = new Dictionary<string, object>();

            NameValueCollection namespaces = null;
            XmlNamespaceManager xnsm = null;
            string prefix = "";

            List<XmlElement> elements = new List<XmlElement>();
            foreach (var element in results.ElementExtensions) {
                elements.Add(element.GetObject<XmlElement>());
            }

            if (options.KeepNamespaces) {

                // Initialize namespaces
                namespaces = new NameValueCollection();
                namespaces.Set("", "http://geojson.org/ns#");
                namespaces.Set("atom", "http://www.w3.org/2005/Atom");
                foreach (var elem in results.ElementExtensions) {
                    XmlReader reader = elem.GetReader();
                    XmlDocument doc = new XmlDocument();
                    doc.Load(reader);
                    xnsm = new XmlNamespaceManager(doc.NameTable);
                    foreach (var names in xnsm.GetNamespacesInScope(XmlNamespaceScope.All)) {
                        namespaces.Set(names.Key, names.Value);
                    }
                }
                fc.Properties.Add("@namespaces", namespaces);
                prefix = "atom:";
            }

            fc.Properties = fc.Properties.Concat(util.ImportXmlDocument(elements.ToArray(), ref namespaces)).ToDictionary(x => x.Key, x => x.Value);

            if (results.Date != null && fc.Properties.ContainsKey(prefix + "updated") == null)
                fc.Properties.Add(prefix + "updated", results.Date.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"));

            if (results.Links != null && results.Links.Count > 0) {
                fc.Links = results.Links;
            }

            if (options.KeepNamespaces == true && fc.Properties.ContainsKey("dc:identifier") == null) {
                namespaces.Set("dc", "http://purl.org/dc/elements/1.1/");
                fc.Properties.Add("dc:identifier", results.Identifier);
            }
            if (options.KeepNamespaces == false && fc.Properties.ContainsKey("identifier") == null) {
                fc.Properties.Add("identifier", results.Identifier);
            }

            if (results.Items != null) {
                foreach (var item in results.Items) {
                    fc.FeatureResults.Add(ImportItem(item));
                }
            }

            return fc;
        }
		public void GetNamespacesInScope ()
		{
			XmlNamespaceManager nsmgr =
				new XmlNamespaceManager (new NameTable ());

			Assert.AreEqual (0, nsmgr.GetNamespacesInScope (l).Count, "#1");
			Assert.AreEqual (0, nsmgr.GetNamespacesInScope (x).Count, "#2");
			Assert.AreEqual (1, nsmgr.GetNamespacesInScope (a).Count, "#3");

			nsmgr.AddNamespace ("foo", "urn:foo");
			Assert.AreEqual (1, nsmgr.GetNamespacesInScope (l).Count, "#4");
			Assert.AreEqual (1, nsmgr.GetNamespacesInScope (x).Count, "#5");
			Assert.AreEqual (2, nsmgr.GetNamespacesInScope (a).Count, "#6");

			// default namespace
			nsmgr.AddNamespace ("", "urn:empty");
			Assert.AreEqual (2, nsmgr.GetNamespacesInScope (l).Count, "#7");
			Assert.AreEqual (2, nsmgr.GetNamespacesInScope (x).Count, "#8");
			Assert.AreEqual (3, nsmgr.GetNamespacesInScope (a).Count, "#9");

			// PushScope
			nsmgr.AddNamespace ("foo", "urn:foo");
			nsmgr.PushScope ();
			Assert.AreEqual (0, nsmgr.GetNamespacesInScope (l).Count, "#10");
			Assert.AreEqual (2, nsmgr.GetNamespacesInScope (x).Count, "#11");
			Assert.AreEqual (3, nsmgr.GetNamespacesInScope (a).Count, "#12");

			// PopScope
			nsmgr.PopScope ();
			Assert.AreEqual (2, nsmgr.GetNamespacesInScope (l).Count, "#13");
			Assert.AreEqual (2, nsmgr.GetNamespacesInScope (x).Count, "#14");
			Assert.AreEqual (3, nsmgr.GetNamespacesInScope (a).Count, "#15");

			nsmgr.AddNamespace ("", "");
			// MS bug - it should return 1 for .Local but it returns 2 instead.
			//Assert.AreEqual (1, nsmgr.GetNamespacesInScope (l).Count, "#16");
			Assert.AreEqual (1, nsmgr.GetNamespacesInScope (x).Count, "#17");
			Assert.AreEqual (2, nsmgr.GetNamespacesInScope (a).Count, "#18");
		}
		public void GetNamespacesInScope ()
		{
			XmlNamespaceManager nsmgr =
				new XmlNamespaceManager (new NameTable ());

			AssertEquals (0, nsmgr.GetNamespacesInScope (l).Count);
			AssertEquals (0, nsmgr.GetNamespacesInScope (x).Count);
			AssertEquals (1, nsmgr.GetNamespacesInScope (a).Count);

			nsmgr.AddNamespace ("foo", "urn:foo");
			AssertEquals (1, nsmgr.GetNamespacesInScope (l).Count);
			AssertEquals (1, nsmgr.GetNamespacesInScope (x).Count);
			AssertEquals (2, nsmgr.GetNamespacesInScope (a).Count);

			// default namespace
			nsmgr.AddNamespace ("", "urn:empty");
			AssertEquals (1, nsmgr.GetNamespacesInScope (l).Count);
			AssertEquals (1, nsmgr.GetNamespacesInScope (x).Count);
			AssertEquals (2, nsmgr.GetNamespacesInScope (a).Count);

			// PushScope
			nsmgr.AddNamespace ("foo", "urn:foo");
			nsmgr.PushScope ();
			AssertEquals (0, nsmgr.GetNamespacesInScope (l).Count);
			AssertEquals (1, nsmgr.GetNamespacesInScope (x).Count);
			AssertEquals (2, nsmgr.GetNamespacesInScope (a).Count);

			// PopScope
			nsmgr.PopScope ();
			AssertEquals (1, nsmgr.GetNamespacesInScope (l).Count);
			AssertEquals (1, nsmgr.GetNamespacesInScope (x).Count);
			AssertEquals (2, nsmgr.GetNamespacesInScope (a).Count);

			nsmgr.AddNamespace ("", "");
			AssertEquals (1, nsmgr.GetNamespacesInScope (l).Count);
			AssertEquals (1, nsmgr.GetNamespacesInScope (x).Count);
			AssertEquals (2, nsmgr.GetNamespacesInScope (a).Count);
		}
        /// <summary>
        /// Get namespaces.
        /// </summary>
        /// <param name="document"></param>
        /// <returns></returns>
        public static IEnumerable<Tuple<string, string>> Namespaces(this XmlDocument document)
        {
            var manager = new XmlNamespaceManager(document.NameTable);
            var ns = manager.GetNamespacesInScope(XmlNamespaceScope.All);

            return ns.Select(x => new Tuple<string, string>(x.Key, x.Value));
        }
        public FeatureCollectionResult ImportFeed(SyndicationFeed feed)
        {
            if (feed == null)
                throw new ArgumentNullException("feed");

            FeatureCollectionResult fc = new FeatureCollectionResult();

            NameValueCollection namespaces = null;
            XmlNamespaceManager xnsm = null;
            string prefix = "";

            if (options.KeepNamespaces) {

                // Initialize namespaces
                namespaces = new NameValueCollection();
                namespaces.Set("", "http://geojson.org/ns#");
                namespaces.Set("atom", "http://www.w3.org/2005/Atom");
                XmlReader reader = feed.ElementExtensions.GetReaderAtElementExtensions();
                xnsm = new XmlNamespaceManager(reader.NameTable);
                foreach (var names in xnsm.GetNamespacesInScope(XmlNamespaceScope.All)) {
                    namespaces.Set(names.Key, names.Value);
                }
                fc.Properties.Add("@namespaces", namespaces);
                prefix = "atom:";
            }

            // Import Feed
            if (feed != null) {
                if ((feed.Authors != null) && (feed.Authors.Count > 0)) {
                    object[] authors = new object[feed.Authors.Count];
                    fc.Properties.Add(prefix + "authors", authors);
                    for (int i = 0; i < feed.Authors.Count; i++) {
                        Dictionary<string,object> author = new Dictionary<string, object>();
                        author.Add(prefix + "name", feed.Authors[i].Name);
                        author.Add(prefix + "email", feed.Authors[i].Email);
                        author.Add(prefix + "uri", feed.Authors[i].Uri);
                        author = author.Concat(util.ImportAttributeExtensions(feed.Authors[i].AttributeExtensions, namespaces, xnsm)).ToDictionary(x => x.Key, x => x.Value);
                        author = author.Concat(util.ImportElementExtensions(feed.Authors[i].ElementExtensions, namespaces)).ToDictionary(x => x.Key, x => x.Value);
                        authors[i] = author;
                    }
                }
                if (feed.BaseUri != null) {
                    fc.Properties.Add(prefix + "uri", feed.BaseUri.ToString());
                }
                if ((feed.Categories != null) && (feed.Categories.Count > 0)) {
                    object[] categories = new object[feed.Categories.Count];
                    fc.Properties.Add(prefix + "categories", categories);
                    for (int i = 0; i < feed.Categories.Count; i++) {
                        Dictionary<string,object> category = new Dictionary<string, object>();
                        category.Add(prefix + "name", feed.Categories[i].Name);
                        category.Add(prefix + "label", feed.Categories[i].Label);
                        category.Add(prefix + "scheme", feed.Categories[i].Scheme);
                        category = category.Union(util.ImportAttributeExtensions(feed.Categories[i].AttributeExtensions, namespaces, xnsm)).ToDictionary(x => x.Key, x => x.Value);
                        category = category.Union(util.ImportElementExtensions(feed.Categories[i].ElementExtensions, namespaces)).ToDictionary(x => x.Key, x => x.Value);
                        categories[i] = category;
                    }
                }
                if ((feed.Contributors != null) && (feed.Contributors.Count > 0)) {
                    object[] contributors = new object[feed.Contributors.Count];
                    fc.Properties.Add(prefix + "categories", contributors);
                    for (int i = 0; i < feed.Contributors.Count; i++) {
                        Dictionary<string,object> contributor = new Dictionary<string, object>();
                        contributor.Add(prefix + "name", feed.Contributors[i].Name);
                        contributor.Add(prefix + "email", feed.Contributors[i].Email);
                        contributor.Add(prefix + "uri", feed.Contributors[i].Uri);
                        contributor = contributor.Union(util.ImportAttributeExtensions(feed.Contributors[i].AttributeExtensions, namespaces, xnsm)).ToDictionary(x => x.Key, x => x.Value);
                        contributor = contributor.Union(util.ImportElementExtensions(feed.Contributors[i].ElementExtensions, namespaces)).ToDictionary(x => x.Key, x => x.Value);
                        contributors[i] = contributor;
                    }
                }
                if (feed.Copyright != null) {
                    fc.Properties.Add(prefix + "rights", feed.Copyright.Text);
                }
                if (feed.Description != null) {
                    fc.Properties.Add(prefix + "content", feed.Description.Text);
                }
                if (feed.Generator != null) {
                    fc.Properties.Add(prefix + "generator", feed.Generator);
                }
                fc.Properties.Add(prefix + "id", feed.Id);
                if (feed.ImageUrl != null)
                    fc.Properties.Add(prefix + "logo", feed.ImageUrl.ToString());
                if (feed.Language != null)
                    fc.Properties.Add(prefix + "language", feed.Language);
                if (feed.LastUpdatedTime != null)
                    fc.Properties.Add(prefix + "updated", feed.LastUpdatedTime);
                if ((feed.Links != null) && (feed.Links.Count > 0)) {
                    fc.Links = feed.Links;
                }
                if (feed.Title != null)
                    fc.Properties.Add(prefix + "title", feed.Title);

                if (feed.Items != null) {
                    foreach (var item in feed.Items) {
                        fc.FeatureResults.Add(ImportItem(item));
                    }
                }

                return fc;

            }
            return null;
        }
        public FeatureResult ImportItem(SyndicationItem item)
        {
            if (item != null) {

                List<XmlElement> elements = new List<XmlElement>();
                foreach (var element in item.ElementExtensions) {
                    elements.Add(element.GetObject<XmlElement>());
                }

                XmlElement geometry = ImportUtils.FindGmlGeometry(elements.ToArray());

                FeatureResult feature = new FeatureResult(Terradue.GeoJson.Geometry.GeometryFactory.GmlToFeature(geometry));

                NameValueCollection namespaces = null;
                XmlNamespaceManager xnsm = null;
                string prefix = "";

                if (options.KeepNamespaces) {

                    // Initialize namespaces
                    namespaces = new NameValueCollection();
                    namespaces.Set("", "http://geojson.org/ns#");
                    namespaces.Set("atom", "http://www.w3.org/2005/Atom");
                    XmlReader reader = item.ElementExtensions.GetReaderAtElementExtensions();
                    xnsm = new XmlNamespaceManager(reader.NameTable);
                    foreach (var names in xnsm.GetNamespacesInScope(XmlNamespaceScope.All)) {
                        namespaces.Set(names.Key, names.Value);
                    }
                    feature.Properties.Add("@namespaces", namespaces);
                    prefix = "atom:";
                }

                // Import Item
                if ((item.Authors != null) && (item.Authors.Count > 0)) {
                    object[] authors = new object[item.Authors.Count];
                    feature.Properties.Add(prefix + "authors", authors);
                    for (int i = 0; i < item.Authors.Count; i++) {
                        Dictionary<string,object> author = new Dictionary<string, object>();
                        author.Add(prefix + "name", item.Authors[i].Name);
                        author.Add(prefix + "email", item.Authors[i].Email);
                        author.Add(prefix + "uri", item.Authors[i].Uri);
                        author = author.Union(util.ImportAttributeExtensions(item.Authors[i].AttributeExtensions, namespaces, xnsm)).ToDictionary(x => x.Key, x => x.Value);
                        author = author.Union(util.ImportElementExtensions(item.Authors[i].ElementExtensions, namespaces)).ToDictionary(x => x.Key, x => x.Value);
                        authors[i] = author;
                    }
                }
                if (item.BaseUri != null) {
                    feature.Properties.Add(prefix + "uri", item.BaseUri.ToString());
                }
                if ((item.Categories != null) && (item.Categories.Count > 0)) {
                    object[] categories = new object[item.Categories.Count];
                    feature.Properties.Add(prefix + "categories", categories);
                    for (int i = 0; i < item.Categories.Count; i++) {
                        Dictionary<string,object> category = new Dictionary<string, object>();
                        category.Add(prefix + "name", item.Categories[i].Name);
                        category.Add(prefix + "label", item.Categories[i].Label);
                        category.Add(prefix + "scheme", item.Categories[i].Scheme);
                        category = category.Union(util.ImportAttributeExtensions(item.Categories[i].AttributeExtensions, namespaces, xnsm)).ToDictionary(x => x.Key, x => x.Value);
                        category = category.Union(util.ImportElementExtensions(item.Categories[i].ElementExtensions, namespaces)).ToDictionary(x => x.Key, x => x.Value);
                        categories[i] = category;
                    }
                }
                if ((item.Contributors != null) && (item.Contributors.Count > 0)) {
                    object[] contributors = new object[item.Contributors.Count];
                    feature.Properties.Add(prefix + "categories", contributors);
                    for (int i = 0; i < item.Contributors.Count; i++) {
                        Dictionary<string,object> contributor = new Dictionary<string, object>();
                        contributor.Add(prefix + "name", item.Contributors[i].Name);
                        contributor.Add(prefix + "email", item.Contributors[i].Email);
                        contributor.Add(prefix + "uri", item.Contributors[i].Uri);
                        contributor = contributor.Union(util.ImportAttributeExtensions(item.Contributors[i].AttributeExtensions, namespaces, xnsm)).ToDictionary(x => x.Key, x => x.Value);
                        contributor = contributor.Union(util.ImportElementExtensions(item.Contributors[i].ElementExtensions, namespaces)).ToDictionary(x => x.Key, x => x.Value);
                        contributors[i] = contributor;
                    }
                }
                if (item.Copyright != null) {
                    feature.Properties.Add(prefix + "rights", item.Copyright.Text);
                }
                if (item.Summary != null) {
                    feature.Properties.Add(prefix + "summary", item.Summary.Text);
                }
                if (item.Content != null) {
                    feature.Properties.Add(prefix + "content", item.Summary.Text);
                }
                feature.Id = item.Id;
                if (item.LastUpdatedTime != null)
                    feature.Properties.Add(prefix + "published", item.LastUpdatedTime);
                if ((item.Links != null) && (item.Links.Count > 0)) {
                    feature.Links = item.Links;
                }
                if (item.Title != null)
                    feature.Properties.Add(prefix + "title", item.Title);

                return feature;
            }

            return null;
        }