public static DateTime?GetDate(IXmlNode el, string xpath, XmlNamespaceManager nsMgr, Uri documentUri)
        {
            if (el == null)
            {
                return(null);
            }
            var node = nsMgr == null
                ? el.SelectSingleNode(xpath)
                : el.SelectSingleNodeNS(xpath, nsMgr.DefaultNamespace);

            if (node == null)
            {
                return(null);
            }
            string rawDate = node.NodeValue.ToString(); // example: 2010-04-02T18:02:55Z

            if (String.IsNullOrEmpty(rawDate))
            {
                return(null);
            }

            DateTime dateTime;

            if (DateTime.TryParse(rawDate, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime))
            {
                return(dateTime);
            }

            return(null);
        }
            private static AtomContentValue ToTextValue(IXmlNode target)
            {
                string type     = "text";
                var    attrType = target.Attributes.SingleOrDefault(a => a.NodeValue == "type");

                if (attrType != null)
                {
                    type = (string)attrType.NodeValue;
                }
                switch (type)
                {
                case "html":
                    return(new AtomContentValue(AtomContentValueType.HTML, target.InnerText.Trim()));

                case "xhtml":
                {
                    XmlNamespaceManager nsMgr = new XmlNamespaceManager(new NameTable());
                    nsMgr.AddNamespace("xhtml", "http://www.w3.org/1999/xhtml");

                    var div = target.SelectSingleNodeNS("xhtml:div", nsMgr.ToNSMethodFormat());
                    if (div != null)
                    {
                        return(new AtomContentValue(AtomContentValueType.XHTML, (string)div.NodeValue));
                    }
                    else
                    {
                        return(new AtomContentValue(AtomContentValueType.XHTML, string.Empty));
                    }
                }

                default:
                case "text":
                    return(new AtomContentValue(AtomContentValueType.Text, target.InnerText.Trim()));
                }
            }
            public override void RemoveAllCategories(IXmlNode node, string categoryScheme, Uri documentUri)
            {
                XmlNamespaceManager nsMgr = new XmlNamespaceManager(new NameTable());

                nsMgr.AddNamespace("dc", DC_URI);
                IXmlNode category;

                while (null != (category = node.SelectSingleNodeNS("dc:subject", nsMgr.ToNSMethodFormat())))
                {
                    category.ParentNode.RemoveChild(category);
                }
            }
Beispiel #4
0
 public static Uri TryGetUri(IXmlNode parentNode, string key, string ns = null)
 {
     return parentNode.SelectSingleNodeNS(key, ns)?.TryReadUri();
 }
Beispiel #5
0
 public static DateTime? TryGetDateTime(IXmlNode parentNode, string subNode, string ns = null)
 {
     return parentNode.SelectSingleNodeNS(subNode, ns)?.TryReadDateTime();
 }
        public static string GetUrl(IXmlNode el, string xpath, XmlNamespaceManager nsMgr, Uri documentUri)
        {
            if (el == null)
            {
                return(null);
            }
            var node = nsMgr == null
                ? el.SelectSingleNode(xpath)
                : el.SelectSingleNodeNS(xpath, nsMgr.DefaultNamespace);

            if (node == null)
            {
                return(null);
            }
            string rawUrl = node.NodeValue.ToString();

            if (rawUrl == null || rawUrl.Length == 0)
            {
                return(null);
            }

            // optimize for common case of absolute path
            if (rawUrl.StartsWith("http", StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    return(UrlHelper.SafeToAbsoluteUri(new Uri(rawUrl)));
                }
                catch { }
            }

            ArrayList ancestors = new ArrayList();

            var parent = node.ParentNode;

            while (parent != null)
            {
                ancestors.Add(parent);
                parent = parent.ParentNode;
            }

            ancestors.Reverse();

            Uri uri = documentUri;

            foreach (var anc in ancestors)
            {
                if (anc is XmlElement)
                {
                    XmlElement baseEl = (XmlElement)anc;
                    if (baseEl.Attributes.Any(a => a.LocalName == "xml:base"))
                    {
                        string thisUri = baseEl.GetAttribute("xml:base");
                        if (uri == null)
                        {
                            uri = new Uri(thisUri);
                        }
                        else
                        {
                            uri = new Uri(uri, thisUri);
                        }
                    }
                }
            }

            if (uri == null)
            {
                return(UrlHelper.SafeToAbsoluteUri(new Uri(rawUrl)));
            }
            else
            {
                return(UrlHelper.SafeToAbsoluteUri(new Uri(uri, rawUrl)));
            }
        }
            private static AtomContentValue ToTextValue(IXmlNode node)
            {
                string type = (string)node.Attributes.SingleOrDefault(a => a.NodeName == "type").NodeValue;

                string mode = (string)node.Attributes.SingleOrDefault(a => a.NodeName == "mode")?.NodeValue;

                if (string.IsNullOrEmpty(mode))
                {
                    mode = "xml";
                }

                string content;

                switch (mode)
                {
                case "escaped":
                    content = node.InnerText;
                    break;

                case "base64":
                    content = Encoding.UTF8.GetString(Convert.FromBase64String((string)node.NodeValue));
                    break;

                default:
                case "xml":
                    content = node.InnerText;
                    if (type == string.Empty && node.SelectSingleNode("./*") != null)
                    {
                        type = "application/xhtml+xml";
                    }
                    break;
                }

                AtomContentValue tv;

                switch (type)
                {
                case "text/html":
                    tv = new AtomContentValue(AtomContentValueType.HTML, content);
                    break;

                case "application/xhtml+xml":
                    XmlNamespaceManager nsMgr = new XmlNamespaceManager(new NameTable());
                    nsMgr.AddNamespace("xhtml", "http://www.w3.org/1999/xhtml");

                    if (mode == "xml")
                    {
                        var div = node.SelectSingleNodeNS("xhtml:div", nsMgr.ToNSMethodFormat());
                        if (div != null)
                        {
                            tv = new AtomContentValue(AtomContentValueType.XHTML, (string)div.NodeValue);
                        }
                        else
                        {
                            tv = new AtomContentValue(AtomContentValueType.XHTML, string.Empty);
                        }
                    }
                    else
                    {
                        tv = new AtomContentValue(AtomContentValueType.XHTML, content);
                    }
                    break;

                default:
                case "text/plain":
                    tv = new AtomContentValue(AtomContentValueType.Text, content);
                    break;
                }
                return(tv);
            }