You must use the name manager or XsltContext. This request has a prefix, variable, or user-defined function. in HTMLAgilityPack

I have the following content in an Html Document

 <opf:metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
    <dc:title>Book Title</dc:title>
    <dc:language>en-us</dc:language>
    <meta name="cover" content="My_Cover" xmlns="" />
    <dc:identifier id="BookId" opf:scheme="ISBN">123456789</dc:identifier>
    <dc:creator>Author Name</dc:creator>
    <dc:publisher>amazon.com</dc:publisher>
    <dc:subject>amazon.com</dc:subject>
    <dc:date>2014-02-28T00:00:00+05:30</dc:date>
  </opf:metadata>

I need to change the attribute value <dc:date>to the current date. I am using HtmlAgilityPack and the code below

var html1 = new HtmlAgilityPack.HtmlDocument();
             html1.LoadHtml(OPFFile);

             var links1 = html1.DocumentNode.SelectNodes("opf:metadata");

             foreach (var link in links1)
             {

                 link.Attributes["dc:date"].Value = DateTime.Now.ToString("yyyy-MM-dd");
             }

             var builder1 = new StringBuilder();
             using (var writer = new StringWriter(builder))
             {
                 html1.Save(writer);
             }
             OPFFile = builder1.ToString();
             File.WriteAllText(@"D:\FindImageInFile\FindImageInFile\OPF.html", OPFFile);

But when I tried to convert, I get this error. Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function. How to change its value?

+4
source share
1 answer

It seems that HtmlAgilityPack not support XPath prefix .

html- ( xml), XDocument XmlDocument, /.

, <cd:date> xml , XDocument:

var xdoc = XDocument.Parse(OPFFile);
//or if OPFFile is file path use : XDocument.Load(OPFFile);
XNamespace dc = "http://purl.org/dc/elements/1.1/";
var date = (string)xdoc.Root.Element(dc + "date");
+2

Source: https://habr.com/ru/post/1529741/


All Articles