This project is read-only.

Example Code

HTML


INodeDocument hdoc = new HTMLDocument(@"
<html>
    <head>
        <title>news</title>
    </head>
    <body>
        <h1>breaking news</h1>
        <ul>
            <li class=""news"">
                <h3>title1</h3>
                <div>body1</div>
            </li>
            <li class=""news"">
                <h3>title2</h3>
                <div>body2</div>
            </li>
            <li class=""news"">
                <h3>title3</h3>
                <div>body3</div>
            </li>
        </ul>
    </body>
</html>");

//find all nodes of the type 'li' and with the 'class' attribute set to 'news'
var newsNodes = hdoc.Find(node => node.Name == "li" && node.Attributes["class"] == "news");

foreach (var newsNode in newsNodes)
  Console.WriteLine(newsNode.Nodes["div"].Text);

CSS


INodeDocument cssdoc = new CssDocument(@"
    body
    {
        font-family:arial;
        background-color:#000000;
        margin:50px;
    }
");

//find and print the margin style for the body element
Console.WriteLine(cssdoc.FindFirst("body").FindFirst("margin").Text);

HTML + CSS


INodeDocument doc = new HTMLDocument(@"
    <html>
        <head>
            <style>
                /*this is a comment in the css*/
                body
                {
                    padding:0px;
                    margin:50px;
                    font-family:arial;
                    background-color:red;
                }
            </style>
        </head>
    <body>

    </body>
    </html>
", true, false);

//find the body element of the html document
var bnode = doc.FindFirst("html").ChildNodes.Where(node => node.Name == "body").Single();

//find and print the css margin style set for the body element
Console.WriteLine("{0}", doc.FindFirst("style").FindFirst(bnode.Name).FindFirst("margin").Text);

Javascript

INodeDocument doc = new JsDocument(@"

    /*
    * just some silly object notation person
    */
    var PersonObject =
    {
        //just some silly function
        SaySomething:function(text)
        {
            var before = 'I was told to say: ';

            alert(before + text);

            var secret = 'I will not say this out loud';
        },
        Height:100,
        Weight:56,
        EyeColor:'Blue'
    }

");

//find and print the secret variable
Console.WriteLine("the secret is: {0}", doc.FindFirst("PersonObject").FindFirst("SaySomething").FindFirst("secret").Text);

//this prints the Height, Weight and EyeColor properties of the PersonObject
foreach(var node in doc.FindFirst("PersonObject").ChildNodes.Where(node => node.HasValue))
    Console.WriteLine("{0} = {1}", node.Name, node.Text);

HTML + Javascript


var doc = new HTMLDocument(@"
<!doctype html>
<html>
    <head>
        <script type=""text/javascript"">

            function LinkClicked(e)
            {
                var text = ""you have clicked a link"";
                alert(text);
            }             

            function ButtonClicked(e)
            {
                var text = ""you have clicked a button"";
                alert(text);
            }          

        </script>
    </head>

    <body>

        <a href=""#"" onclick=""LinkClicked"">I am a link, click me!</a>

        <input type=""button"" value=""click me too!"" onclick=""ButtonClicked""/>

        <img src=""404.bmp"" onclick=""ImgClicked"" alt=""oops, no image or js function found""/>

    </body>                
</html>

", false, true);

//find all nodes that has the onclick attribute set and fetch the onclick values
var onClickFunctionNames = from node in doc.Find(n => n.Attributes["onclick"] != null) select node.Attributes["onclick"];

//loop trough all the fetched onclick values, find the matching javascript function and print the text variable
foreach (var onClickFunctionName in onClickFunctionNames)
    Console.WriteLine("text: {0}", doc.Find(n => n.Name == "script" && n.Attributes["type"] == "text/javascript").FindFirst(onClickFunctionName).FindFirst("text").Text);

[OUTPUT]

text:  "you have clicked a link"
text:  "you have clicked a button"
text: 

XML


INodeDocument xdoc = new XmlDocument(@"<?xml version=""1.0"" encoding=""UTF-8""?>
        <data>
            <topnews>
                <news title=""headline1"">body1</news>
                <news title=""headline2"">body2</news>
                <news title=""headline3"">body3</news>
            </topnews>
        </data>");

//find all nodes that have the title attribute set
var newsNodes = xdoc.Find(newsnode => newsnode.Attributes["title"] != null);

foreach (var newsNode in newsNodes)
  Console.WriteLine(newsNode.Text);

JSON


INodeDocument jdoc = new JSONDocument(@"
{topnews:[
    {title:'headline1',text:'body1'},
    {title:'headline1',text:'body2'},
    {title:'headline1',text:'body3'}
]}");

//find all nodes that have a child node named title
var newsNodes = jdoc.Find(newsnode => newsnode.Nodes["title"] != null);

foreach (var newsNode in newsNodes)
  Console.WriteLine(newsNode.Nodes["text"].Text);

Last edited Apr 11, 2010 at 8:06 PM by creamdog, version 2

Comments

No comments yet.