0

I have a really bad problem. I have a lot of records in database which are in Persian language. For some reasons, I load them, make some changes to them (by HtmlAgilityPack), and then save them back to db. But all strings gone to ????????????? symbols. Is there any way to getting them back? Here is my code snippet:

private void SomeStartMethod(){
    var str = ReadFromDb();
    str = MakeChanges(str);
    SaveToDbAgain(str);
}

private string MakeChanges(string contentHtml) {
    var reader = new StringReader(contentHtml);

    var doc = new HtmlDocument();
    doc.Load(reader);
    var nodes = doc.DocumentNode.SelectNodes("//a[@href]");
    if (nodes == null || !nodes.Any())
        return contentHtml;
    foreach (var link in nodes) {
        // do some stuffs here...
    }

    using (var memoryStream = new MemoryStream()) {
        doc.Save(memoryStream);
        memoryStream.Seek(0, SeekOrigin.Begin);
        var streamReader = new StreamReader(memoryStream /* here was my mistake, I forgot to put , Encoding.UTF8 here */);
        var result = streamReader.ReadToEnd();
        return result;
    }
}
amiry jd
  • 27,021
  • 30
  • 116
  • 215
  • No, the characters have been replaced by the `?` when C#/.NET couldn't load them correctly. You'll need to restore the strings from another copy of the data (if available.) – OSborn Sep 02 '14 at 01:43
  • You say your error was forgetting to specify Encoding.UTF8 when you created the StreamReader object, but this seems to indicate that that should be defult: http://msdn.microsoft.com/en-us/library/yhfzs7at%28v=vs.110%29.aspx – RenniePet Sep 02 '14 at 02:04
  • @RenniePet So may be I am wrong and I should set HtmlDocument encoding? However, what can I do now? do you have any idea please? – amiry jd Sep 02 '14 at 02:14
  • Sorry, no, wish I did. (Some kinds of databases maintain a transaction log that allow rollbacks, but I assume you know that.) – RenniePet Sep 02 '14 at 02:17
  • @RenniePet ): ): The `Restore/Transaction Log` menu item is unavailable. I'm using a shared hosting. – amiry jd Sep 02 '14 at 02:25
  • You are totally sure the data has become corrupted? Those question marks you're seeing are not due to viewing the data in an ASCII-only viewer? – RenniePet Sep 02 '14 at 02:29
  • @RenniePet well it seems they are corrupted ): I'm using the db in a website. also when I get `var b =Encoding.UTF8.GetBytes(txt);`, it seems all bytes have value 63. I'll get killed. Thank you for your help. But I think I have no option. – amiry jd Sep 02 '14 at 02:33
  • Change your name and join the French Foreign Legion? – RenniePet Sep 02 '14 at 02:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/60423/discussion-between-javad-amiry-and-renniepet). – amiry jd Sep 02 '14 at 02:40

0 Answers0