当前位置: 首页>>代码示例>>C#>>正文


C# HtmlDocument.SelectNodes方法代码示例

本文整理汇总了C#中HtmlAgilityPack.HtmlDocument.SelectNodes方法的典型用法代码示例。如果您正苦于以下问题:C# HtmlDocument.SelectNodes方法的具体用法?C# HtmlDocument.SelectNodes怎么用?C# HtmlDocument.SelectNodes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在HtmlAgilityPack.HtmlDocument的用法示例。


在下文中一共展示了HtmlDocument.SelectNodes方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ValidateHtml

        public ValidationError[] ValidateHtml(HtmlDocument document)
        {
            var records = new List<ValidationError>();

            var nodes = document.SelectNodes("//h1");

            if (nodes.Count == 0)
            {
                records.Add(new ValidationError("There must be at least one H1 tag on the page"));
            }

            return records.ToArray();
        }
开发者ID:mahendramavani,项目名称:Naak,代码行数:13,代码来源:AtLeastOneH1.cs

示例2: ValidateHtml

        public ValidationError[] ValidateHtml(HtmlDocument document)
        {
            var records = new List<ValidationError>();

            string formElementXPath = "//input[@type='image'][not(@alt) or @alt='']";

            var imageButtonsWithoutAlt = document.SelectNodes(formElementXPath);

            foreach (var imageButton in imageButtonsWithoutAlt)
            {
                string message = string.Format("Image input missing alt text: {0}", imageButton.OuterHtml);
                records.Add(new ValidationError(message, imageButton.Line, imageButton.LinePosition));
            }

            return records.ToArray();
        }
开发者ID:mahendramavani,项目名称:Naak,代码行数:16,代码来源:ImageInputsHaveAltText.cs

示例3: ValidateHtml

        public ValidationError[] ValidateHtml(HtmlDocument document)
        {
            var records = new List<ValidationError>();

            var textDefinition = new {TagName = "text", Description = "Textbox", IsInput = true};
            var passwordDefinition = new {TagName = "password", Description = "Password textbox", IsInput = true};
            var checkboxDefinition = new {TagName = "checkbox", Description = "Checkbox", IsInput = true};
            var selectDefinition = new { TagName = "select", Description = "Select list", IsInput = false };
            var textAreaDefinition = new { TagName = "textarea", Description = "Text area", IsInput = false };

            var definitions = new[] {textDefinition, passwordDefinition, checkboxDefinition, selectDefinition, textAreaDefinition};

            foreach (var definition in definitions)
            {
                var formElementXPath = definition.IsInput ? string.Format("//input[@type='{0}']", definition.TagName) : string.Format("//{0}", definition.TagName);

                var elements = document.SelectNodes(formElementXPath);

                foreach (var element in elements)
                {
                    var idAttribute = element.Attributes["id"];

                    HtmlNode correspondingLabel = null;

                    if (idAttribute != null)
                    {
                        var elementId = idAttribute.Value;
                        var xpath = string.Format("//label[@for='{0}']", elementId);

                        correspondingLabel = document.SelectSingleNode(xpath);
                    }

                    if (correspondingLabel == null)
                    {
                        var message = string.Format("{0} missing correpsonding label: {1}", definition.Description, element.OuterHtml);
                        records.Add(new ValidationError(message));
                    }
                }
            }

            return records.ToArray();
        }
开发者ID:mahendramavani,项目名称:Naak,代码行数:42,代码来源:FormElementsHaveLabels.cs

示例4: ValidateHtml

        public ValidationError[] ValidateHtml(HtmlDocument document)
        {
            var records = new List<ValidationError>();

            var existingLinks = new List<Link>();
            const string formElementXPath = "//a";

            var linksNodeList = document.SelectNodes(formElementXPath);

            if (linksNodeList != null)
                foreach (var currentNode in linksNodeList)
                {
                    var urlAttribute = currentNode.Attributes["href"];
                    var titleAttribute = currentNode.Attributes["title"];
                    var nameAttribute = currentNode.Attributes["name"];
                    var linkText = currentNode.InnerText;

                    var url = urlAttribute == null ? string.Empty : urlAttribute.Value;
                    var title = titleAttribute == null ? string.Empty : titleAttribute.Value;
                    var name = nameAttribute == null ? string.Empty : nameAttribute.Value;

                    var link = new Link(url, linkText, title, name);

                    var matches = from c in existingLinks
                                  where c.Url != link.Url && c.Text == link.Text && c.Title == link.Title && c.Name == link.Name
                                  select c;

                    if (matches.Count() == 0)
                        existingLinks.Add(link);
                    else
                    {
                        records.Add(
                            new ValidationError(
                                string.Format(
                                    "Link text does not make sense out of context,link is the same as another link on the page, but links to a different location: {0}",
                                    currentNode.OuterHtml)));
                    }
                }

            return records.ToArray();
        }
开发者ID:mahendramavani,项目名称:Naak,代码行数:41,代码来源:ContextOfLinkTextMustMakeSense.cs

示例5: ValidateHtml

        public ValidationError[] ValidateHtml(HtmlDocument document)
        {
            var records = new List<ValidationError>();

            var labels = document.SelectNodes("//label");

            foreach (var label in labels)
            {
                var forAttribute = label.Attributes["for"];
                HtmlNode relatedElement = null;

                if (forAttribute != null)
                {
                    string labelId = forAttribute.Value;
                    string xpath = string.Format("//input[@id='{0}']", labelId);

                    relatedElement = document.SelectSingleNode(xpath);

                    if (relatedElement == null)
                    {
                        xpath = string.Format("//select[@id='{0}']", labelId);
                        relatedElement = document.SelectSingleNode(xpath);
                    }

                    if (relatedElement == null)
                    {
                        xpath = string.Format("//textarea[@id='{0}']", labelId);
                        relatedElement = document.SelectSingleNode(xpath);
                    }
                }

                if (relatedElement == null)
                {
                    string message = string.Format("Label does not relate to a form control: {0}", label.OuterHtml);
                    records.Add(new ValidationError(message));
                }
            }

            return records.ToArray();
        }
开发者ID:mahendramavani,项目名称:Naak,代码行数:40,代码来源:LabelsRelateToFormElements.cs

示例6: ValidateHtml

        public ValidationError[] ValidateHtml(HtmlDocument document)
        {
            var records = new List<ValidationError>();

            var nodes = document.SelectNodes("//table");

            if (nodes != null)
                foreach (var node in nodes)
                {
                    var tableHeaders = node.SelectNodes("tr/th");
                    var tableHeadersWithThead = node.SelectNodes("thead/tr/th");
                    if (tableHeaders.Count < 1 && tableHeadersWithThead.Count < 1)
                    {
                        string message =
                            string.Format(
                                "Layout table detected - if the table is a data table, use TH for the column or row headers. Otherwise, use CSS for layout: {0}",
                                node.OuterHtml);
                        records.Add(new ValidationError(message));
                    }
                }
            return records.ToArray();
        }
开发者ID:mahendramavani,项目名称:Naak,代码行数:22,代码来源:TablesHaveColumnHeaders.cs

示例7: ValidateHtml

        public ValidationError[] ValidateHtml(HtmlDocument document)
        {
            var records = new List<ValidationError>();
            var previousAltText = new List<string>();

            const string formElementXPath = "//img";

            var images = document.SelectNodes(formElementXPath);

            foreach (var currentImage in images)
            {
                var altTextAttribute = currentImage.Attributes["alt"];
                if (altTextAttribute != null)
                {
                    if (previousAltText.Contains(altTextAttribute.Value))
                    {
                        string message = string.Format("Image has duplicate alt text: {0}", currentImage.OuterHtml);
                        records.Add(new ValidationError(message));
                    }
                    previousAltText.Add(altTextAttribute.Value);
                }
            }
            return records.ToArray();
        }
开发者ID:mahendramavani,项目名称:Naak,代码行数:24,代码来源:ImagesDontHaveDuplicateAltText.cs


注:本文中的HtmlAgilityPack.HtmlDocument.SelectNodes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。