Error passing URL as parameter ... JoeBlogs & HtmlAgilityPack

13

Main

namespace ReaderXML
{
    class Program
    {
        static void Main(string[] args)
        {
            var website = LeitorDeXML("http://SITE ORIGEM/").ToArray();
            var total = website.Count();
            for (int i = 1; i < total; i++)
            {
                Postagem(website[i]);
            }
        }

Method for blog post

    private static void Postagem(string website)
        {
            try
            {
                //LINK,USER,SENHA WP
                string link = "http://SITE NOVO";
                string username = "user wp";
                string password = "senha wp";

                //Leitura do HTML
                HtmlWeb web = new HtmlWeb();
                HtmlDocument resultat = web.Load(website);

                //Separar TITULO e CONTEUDO
                string titulopost = resultat.DocumentNode.SelectNodes("//*[contains(@class,'entry-title')]")[0].InnerHtml;
                string conteudo = resultat.DocumentNode.SelectNodes("//*[contains(@class,'entry-content')]")[0].InnerHtml; //ERRO AQUI

                //Entrada no wp
                var wp = new WordPressWrapper(link + "/xmlrpc.php", username, password);
                var post = new Post();

                //Categoria -- Não está funcionando ainda a parte da criação da categoria

                int website_corpo = 25;
                string categoria = website.Substring(website_corpo);
                int indexof_barra = categoria.IndexOf("/");
                int comeco_cat = 0;
                string categoria_f = categoria.Substring(comeco_cat, indexof_barra);

                //Data

                post.DateCreated = DateTime.Today.AddHours(0);

                //Postagem
                post.Title = titulopost;
                post.Body = conteudo;

                wp.NewPost(post, true);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e);
                Console.ReadKey();
            }
        }
    }
}

The "ReadModel" method reads a declared sitemap in MAIN and returns me the latest urls.

The problem is that when I pass this url as a parameter, at the time it takes the "content string" it goes no further.

Give the following error:

  

Error: System.NullReferenceException: Object reference not set to   an instance of an object

    
asked by anonymous 20.05.2015 / 22:15

5 answers

1

You are loading the html in this line below, to simulate the problem, you would need the contents of the "website" variable. HtmlDocument resultat = web.Load(website);

Anyway, it looks like the xpath passed below does not find the element in HTML and you are trying to access the first index string conteudo = resultat.DocumentNode.SelectNodes("//*[contains(@class,'entry-content')]")[0].InnerHtml;

You can confirm that xpath did not find anything with this code int totalEntryContent = resultat.DocumentNode.SelectNodes("//*[contains(@class,'entry-content')]").Count;

If you want help to resolve xpath, share the contents of the website variable.

    
13.01.2016 / 03:06
0

The line of code

string conteudo = resultat.DocumentNode.SelectNodes("//*[contains(@class,'entry-content')]")[0].InnerHtml; 

Apparently you are not finding the entry-content div on the page. Make sure this div actually exists on the page. If it does not exist, then that's why it's not being found.

However, if the entry-content div does exist, then the problem is in the code that selects that div.

Try to find the div by following the form below, with linq. It searches the page for a div whose class attribute contains "entry-content":

var conteudo = resultat.DocumentNode.Descendants("div").Where(d => d.Attributes.Contains("class") && d.Attributes["class"].Value.Contains("entry-content") )[0].InnerHtml;
    
11.07.2015 / 04:03
0

Before making the request, the parameter must be encoded in Base64. When getting the request parameter do the decode of the parameter with Base64 as well.

    
23.10.2015 / 21:04
0

I may be talking bullshit, but try putting ex:

var website = LeitorDeXML(new Uri("http://SITE ORIGEM/")).ToArray();
    
18.06.2016 / 04:36
0

I do not know exactly what code you're using to read xml, but you can use the System.Xml library to do this quickly:

String URLString = " http://seu.site.com/SiteMap.xml";
XmlTextReader reader = new XmlTextReader (URLString);

Read more about this solution at PROCEDURES: Read XML Data in a URL Using Visual C # .NET

    
01.09.2016 / 18:22