Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

UWP: HtmlDocument in .Net Core Anwendungen

29.06.2018 (👁5707)


Wie kann man das HtmlDocument in .Net Core und UWP verwenden.

Lösung: HtmlAgilityPack

Hierzu kann man das NuGet Package HtmlAgilityPack vn ZZZ Projects installieren.

This is an agile HTML parser that builds a read/write DOM and supports plain XPATH or XSLT (you actually don't HAVE to understand XPATH nor XSLT to use it, don't worry...).

It is a .NET code library that allows you to parse "out of the web" HTML files.

The parser is very tolerant with "real world" malformed HTML. The object model is very similar to what proposes System.Xml, but for HTML documents (or streams).

 

 

Wenn man das HtmlAgilityPack installiert hat, muss man anschliessend nur noch einen Verweis in die MainPage einfügen

//< using >
using HtmlAgilityPack;          //HtmlDocument
using System.Net;               //*WebRequest
using System.Threading.Tasks;   //*Task
//</ using > 

 

Anschliessend kann man den Code in die UWP Anwendung einbetten

Komplettes Code Beispiel zum Lesen einer Webseite in ein HTMLDocument

private async Task<HtmlDocument> Web_Get_HtmlDocument(string sURL)

        {

            //------------< fx_read_Page() >------------

            //* get the HTML Document of a website-URL      

            try

            {

                //-< init >-

                //< WebRequest and Response >

                WebRequest objRequest = WebRequest.Create(sURL);

                WebResponse objResponse = await objRequest.GetResponseAsync();

                //</ WebRequest and Response >

 

                //< Stream and Reader >

                Stream objDataStream = objResponse.GetResponseStream();

                StreamReader TextReader = new StreamReader(objDataStream);

                //</ Stream and Reader >

                //-</ init >-

 

                //< download >

                //* Read Website to local String

                string sHTML = TextReader.ReadToEnd();

                //</ download >

 

                //< get HTMLdocument >

                //*create and load to local HtmlDocument

                HtmlDocument doc = new HtmlDocument();

                doc.LoadHtml(sHTML);

                //</ get HTMLdocument >

 

                //< output >

                return doc;

                //</ output >

            }

            catch (Exception)

            {

                return null;

            }

 

            //------------</ fx_read_Page() >------------

        }