I am working on project using asp.net mvc4 C# . I want to change some html element

attributes by c# like width , height etc. I have a simple (_Layout.cshtml) file.

<html>
    <head>
        <link href="@Url.Content("file.css")" rel="stylesheet" type="text/css" />
        <link href="@Url.Content("html.css")" rel="stylesheet" type="text/css" />
        <body>
        </body>
    </head> 
</html>

So i am using html agility pack to load and save this file

HtmlDocument doc= new HtmlDocument();

doc.load("_Layout.cshtml");
List<string> linklist = new List<string>();
scriptlist.Add("html.css");
scriptlist.Add("Html1.css");
HtmlNodeCollection _node = doc.DocumentNode.SelectNodes("//link");

foreach (HtmlNode Node in _node)
{
    foreach (var sName in linklist)
    {
        if (Node.OuterHtml.Contains(Name.ToLower()))
        {
            Console.WriteLine(Node.OuterHtml);
            var linkNode = HtmlNode.CreateNode("<link href=" + "@Url.Content(" + '"' + "~/links/../" + Name + '"' + ")" + "/>");
            HeadNode.ReplaceChild(linkNode, Node);
        }
    }                
}

doc.save("_Layout.cshtml");

After saving file output is like this

<html>
    <head>
        <link href="@Url.Content("file.css")"="" rel="stylesheet" type="text/css" />

        <link href="@Url.Content("html.css")" />
        <body>
        </body>
    </head>
</html>

In (link href) saving some extra characters like ="

How can i avoid this problem .. Is there any other solution for parse html in c# for asp.net mvc.

Actually I want to Replace new path to give href attribute

Related posts

Recent Viewed