How to write an xpath expression for :
<script src="../../Scripts/ex.js"></script>
which will match the src value (../../Scripts/ex.js). Because i want to relace ../../Scripts with some other string.
How to write an xpath expression for :
<script src="../../Scripts/ex.js"></script>
which will match the src value (../../Scripts/ex.js). Because i want to relace ../../Scripts with some other string.
you can go with the following code.
TextReader tr = new StringReader(response);
XElement xelement = XElement.Load(tr);
var Script = xelement.Descendants("script").ToList();
if (Script.Count() > 0)
{
Script.First().Attribute("src").Value = "../../Scripts/NewEXWithReplace.js";
}
string with another, not the src of scriptsrc in source document, result will be only new string instance, I assume that OP wants to replace this src in actual document not to obtain modified string.script but I think that you should chage only scripts that has ../../Scripts/ex.js in the src attribute also I think it should be done for all occurrences...You can also use HtmlAgilityPack which allows you to access attributes usefully codeplex tool.
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(@"<script src="../../Scripts/ex.js"></script>" />");
foreach (var script in doc.DocumentNode.Descendants("script"))
{
script.Attributes["src"].Value = "loading.gif";
}
Another way is to use linq to xml
I know you said you wanted an XPath expression, but what about a simple string replace?
e.g.
string s = "<script src=\"../../Scripts/ex.js\"></script>";
string newS = s.Replace("../../Scripts", "../../Scripts2"); // Or whatever you want to replace it with