0

How can i get the XHtml code in c#?

I'm loading the web page myweb.Source = new Uri(@"https://" + @urlstring.Text);

and i'm saving the html in string

WebClient a = new WebClient();
byte[] data = a.DownloadData(myweb.Source.ToString());
string ab = Encoding.ASCII.GetString(data);

now i'm looking for the video Url path

string id = "";
int index = ab.IndexOf("<video")
int indexe = ab.IndexOf("</video>");
for (int i = index; i <= indexe + 100; i++)
   {
       id += ab[i];
   }

the result is

<video class="js-select-menu-off vjs-video"
                data-account="3695997568001"
                data-player="SyDW1dJDG"
                data-embed="default"
                data-video-id=""
                controls></video>
<div class="playback-speed-popover popover dark">
    <div class="inner-popover">

but what i want is this :

<video class="vjs-tech" data-account="3695997568001" 
data-player="SyDW1dJDG" data-embed="default" 
data-video-id="" id="vjs_video_3_html5_api"
poster="blablabla" src="https://hous......"></video>

how can i get the src from the web page ?

2 Answers 2

2

I'm using .NET Core In My Example With Visual Studio Code. Use to following to install HtmlAgilityPack:

dotnet add package HtmlAgilityPack --version 1.11.16

The code below is how i would solve for it:

    static void Main(string[] args)
    {            
        var client = new System.Net.Http.HttpClient();
        var content = client.GetStringAsync(@"https://www.w3schools.com/html/html5_video.asp").Result;
        HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
        document.LoadHtml(content);            
        var  videos = document.DocumentNode.Descendants("video").FirstOrDefault().Attributes["src"].Value;            
    }
Sign up to request clarification or add additional context in comments.

5 Comments

ibb.co/7tDP6nB i have the same error its loding the html and i need to load XHtml for this ibb.co/dJk4996
var videos = document.DocumentNode.Descendants("video").FirstOrDefault().Attributes["src"].Value;
you have the wrong tag in the screenshot above if you want the value from the src attribute. try it with the line i supplied above.
are you logged in when you are inspecting the web page? I'm wondering if html elements get added when you login to the website. I only see this html markup on the link provided on the other answer <div class="video-player-module"> <video class="js-select-menu-off vjs-video" data-account="3695997568001" data-player="SyDW1dJDG" data-embed="default" data-video-id="" controls></video>
also after login i still cant see the src but i see the video in the app
0

It's easy if you use HtmlAgilityPack with XPath.

Paste below code to run in here: https://dotnetfiddle.net/Vtwi7g

// @nuget: HtmlAgilityPack

using System;
using System.Xml;
using HtmlAgilityPack;

public class Program
{
    public static void Main()
    {
        var html = @"https://www.w3schools.com/html/html5_video.asp";

        HtmlWeb web = new HtmlWeb();

        var htmlDoc = web.Load(html);

        var node = htmlDoc.DocumentNode.SelectSingleNode("//video");

        Console.WriteLine("Node Name: " + node.Name + "\n" + node.OuterHtml + "\n Node id is: " + node.Attributes["id"].Value);     
    }
}

The result will be:

Node Name: video
<video id="video1" style="width:600px;max-width:100%;" controls="">
    <source src="mov_bbb.mp4" type="video/mp4">
    <source src="mov_bbb.ogg" type="video/ogg">
    Your browser does not support HTML5 video.
  </video>
 Node id is: video1

In your case, you just change the attribute to poster instead of id. For more information, check out this document https://html-agility-pack.net/documentation

13 Comments

its return null :(
You didn't change the attribute "id" in my sample to "poster" yet. It's null may be the "id" is not exist. Can you post your url so I can check it?
i'm working on app for myself to download video's from skillshare this the URL that i'm trying to asses skillshare.com/classes/…
i deleted the id and it's the same resalt that i have ibb.co/bPbtrPD
the poster is with in the <video > so its dont have source
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.