public static T GetAttribute <T>(XContainer doc, string item, [CallerMemberName] string attribute = "")
        {
            T result = default(T);

            if (doc != null)
            {
                var value = doc.DescendantNodes()
                            .Where(n => (n.NodeType == XmlNodeType.Element))
                            .Select(n => n as XElement)
                            .Where(n => n.Name.LocalName.ToString() == item)
                            .Select(n => n)
                            .Where(n => n.Attributes(attribute).Count() > 0)
                            .Select(n => n.Attribute(attribute).Value);
                try
                {
                    if (value.Count() > 0)
                    {
                        var converter = TypeDescriptor.GetConverter(typeof(T));
                        if (converter != null)
                        {
                            result = (T)converter.ConvertFromString(value.ElementAt(0));
                        }
                    }
                }
                catch (NotSupportedException)
                {
                }
            }

            return(result);
        }
Esempio n. 2
0
 /// <summary>
 /// Returns a collection of the descendant comment nodes for this document or element, in document order.
 /// </summary>
 /// <param name="xml">The xml from which to get the descendant comment nodes.</param>
 public static IEnumerable <XComment> DescendantComments(this XContainer xml)
 {
     return(xml
            .DescendantNodes()
            .Where(n => n.NodeType == XmlNodeType.Comment)
            .Cast <XComment>());
 }
Esempio n. 3
0
        /// <summary>
        /// Returns a collection of the descendant nodes for this container.
        /// </summary>
        /// <param name="self"></param>
        /// <returns></returns>
        public static IEnumerable <XNode> DescendantNodesAndSelf(this XContainer self)
        {
            yield return(self);

            foreach (var node in self.DescendantNodes())
            {
                yield return(node);
            }
        }
Esempio n. 4
0
 public static void TrimText(this XContainer @this)
 {
     foreach (var decendant in @this.DescendantNodes())
     {
         var decendantText = decendant as XText;
         if (decendantText != null)
         {
             decendantText.Value = decendantText.Value.Trim();
         }
     }
 }
Esempio n. 5
0
        public static string ToCleanText(this XContainer data)
        {
            var text = "";

            foreach (var elem in data.Descendants()
                     .Where(x => x.Name == "script" || x.Name == "head")
                     .ToList())
            {
                elem.Remove();
            }

            foreach (var elem in data.Descendants().Where(x => x.Name == "br"))
            {
                elem.Value = Environment.NewLine;
            }
            foreach (var elem in data.Descendants().Where(x => x.Name == "p"))
            {
                elem.Value = Environment.NewLine + elem.Value + Environment.NewLine;
            }

            foreach (XText node in data.DescendantNodes().OfType <XText>())
            {
                text += node.Value + " ";
            }

            bool lexHasNewLine = false;
            bool lexHasSpace   = false;
            var  retBuilder    = new StringBuilder();

            var cutSymbols = new List <char>()
            {
                ' ', '\t', '\r'
            };
            var skipSymbols = new List <char>()
            {
                '\u00A0', '\u20E7'
            };

            foreach (var c in text)
            {
                if (c == '\n')
                {
                    lexHasNewLine = true;
                }
                else if (cutSymbols.Contains(c))
                {
                    lexHasSpace = true;
                }
                else if (skipSymbols.Contains(c))
                {
                }
                else
                {
                    if (lexHasNewLine)
                    {
                        retBuilder.Append(Environment.NewLine);
                    }
                    else if (lexHasSpace)
                    {
                        retBuilder.Append(" ");
                    }

                    retBuilder.Append(c);
                    lexHasNewLine = lexHasSpace = false;
                }
            }
            return(retBuilder.ToString());
        }
Esempio n. 6
0
 public static IEnumerable <XNode> GetDescendantTextNodes(XContainer xml)
 {
     return(xml.DescendantNodes().Where(node => node.NodeType == System.Xml.XmlNodeType.Text));
 }
Esempio n. 7
0
 private static IEnumerable <XElement> Descendants(XContainer doc, string nodeName)
 {
     return(doc.DescendantNodes().OfType <XElement>().Where(n => n.Name == nodeName));
 }
Esempio n. 8
0
 public static string TextContent(this XContainer container)
 {
     return(string.Concat(container.DescendantNodes()
                          .Where(n => n.NodeType == XmlNodeType.Text)
                          .Select(n => (n as XText).Value)));
 }