HAP SetAttributeValue中的语法错误或代码中的错误

时间:2018-03-15 14:29:30

标签: c# syntax-error html-agility-pack

imgs = doc.DocumentNode.SelectNodes("//img");
HtmlNode img in imgs    

string imageIdString = image.Id.ToString();
img.SetAttributeValue("src", "/ImageBrowser/ImageById/" + imageIdString);

我为ID获得了正确的值,但img源保持不变,我无法找到原因

尝试像这样管理它: Need to replace an img src attrib with new value

Edit1:请求的代码

        string input = sectionEditModel.Content;
        string htmlstring = sectionEditModel.Content;
        string htmlstringdecoded = HttpUtility.HtmlDecode(htmlstring);
        HtmlDocument doc = new HtmlDocument();
        List<string> urls = new List<string>();
        DbImgBrowser.Models.Image image = null;
        doc.LoadHtml(htmlstringdecoded);
        var files = new FilesRepository();


        HtmlNodeCollection imgs = new HtmlNodeCollection(doc.DocumentNode);
        imgs = doc.DocumentNode.SelectNodes("//img");
        if (imgs != null && imgs.Count > 0)
            {

                foreach (HtmlNode img in imgs)
                {
                    HtmlAttribute srcs = img.Attributes[@"src"];
                    urls.Add(srcs.Value);
                    {

                        foreach (string Value in urls){

                            string AttrVal = img.GetAttributeValue("src", null);
                            if(AttrVal.Contains("base64"))
                            {
                                byte[] data = Convert.FromBase64String(Value.Substring(Value.IndexOf(",") + 1));

                                var pFolder = files.GetFolderByPath(string.Empty);
                                if (pFolder != null)
                                {
                                    image = new DbImgBrowser.Models.Image()
                                    {
                                        Name = Guid.NewGuid().ToString(),
                                        Folder = pFolder,
                                        Image1 = data
                                    };
                                    files.Db.Images.Add(image);
                                    files.Db.SaveChanges();

                                    string imageIdString = image.Id.ToString();
                                    img.SetAttributeValue("src", "/ImageBrowser/ImageById/" + imageIdString);

                                    files.Db.SaveChanges();
                                }
                            }

Edit2:示例路径:在base64示例图像之前 路径按Url示例/ImageBrowser/Image?path=Test2.PNG

通缉结果src =&#34; ImageBrowser / ImageById /&#34; ID&#34; (1-1000)

Edit3:仍然没有更改所有src

1 个答案:

答案 0 :(得分:0)

答案很简单。 我在本地文档上,但我必须将其返回到内容并保存部分

SectionsRepository.SaveSection(Section sec)