Refresh WebBrowser in Winform

I put a WebBrowser control on my form and use it to load XML files. Here is the code:
OpenFileDialog FileDialog = new OpenFileDialog();
FileDialog.Title = "Open XML Document";
FileDialog.Filter = "XML file (*.xml)|*.xml";

if (FileDialog.ShowDialog() == DialogResult.OK)
I found that if I load the second XML, sometimes it will not refresh itself to show the new XML content. Instead, it still shows the previous one. I google the web to see if there is any other way to prevent the cache. Unfortunately, I found nothing. But this post inspires me and so I create a workaround.
private void ReloadXml(string xmlPath)
  panelShowXML.Controls.Clear(); //Clear controls first.
  WebBrowser XMLBrowser = new WebBrowser();
  panelShowXML.Controls.Add(XMLBrowser); //Add the WebBrowser control to the Panel.
  XMLBrowser.Dock = DockStyle.Fill; //Fill the full Panel so that it looks like a WebBrowser control.
  XMLBrowser.Navigate(xmlPath); //Load the XML file.
This way we can make sure the WebBrowser control always shows the latest content. It works fine for me, and I hope it works for you as well.

No comments:

Post a Comment