CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Mar 2003
    Posts
    145

    Displaying an Xml file in a Windows form application

    Is there a way to display an Xml file in a Windows form application; just like the IE browser does?

  2. #2
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Lightbulb Re: Displaying an Xml file in a Windows form application

    Code:
                string s = File.ReadAllText("test.xml");
                textbox1.text = s;

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Displaying an Xml file in a Windows form application

    You can also put an IEBrowser control inside the form and load it via the control.

    That way you'll be able to expand and collapse the nodes.

  4. #4
    Join Date
    Mar 2003
    Posts
    145

    Re: Displaying an Xml file in a Windows form application

    Quote Originally Posted by Arjay
    You can also put an IEBrowser control inside the form and load it via the control.
    That way you'll be able to expand and collapse the nodes.
    I would like the user to be able to expand and collapse the Xml nodes, so this sounds like a good solution.
    But, how can I put an IEbrowser control inside the form?

  5. #5
    Join Date
    Jan 2006
    Location
    18° 32' N / 73° 52' E
    Posts
    416

    Thumbs up Re: Displaying an Xml file in a Windows form application

    Use the WebBrowser Control.

    Goto : Add Reference, Under COM tab, choose WebBrowser. (System.Windows.Forms)

    You will find this control in the toolbox after adding it.

    Now just place it on the form.

    MMH.

  6. #6
    Join Date
    Mar 2003
    Posts
    145

    Re: Displaying an Xml file in a Windows form application

    Thanks.
    Now I have a WebBrowser object on my form.
    I tried the following just to see if someting happens, but there was no result.
    Code:
    StreamReader stream = new StreamReader(fileName);
    this.webBrowser1.DocumentStream = stream.BaseStream;
    Is this the way to go?

    My intention is to display the contents of a DataSet in an Xml format, before the data is actually saved to an Xml file.

  7. #7
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: Displaying an Xml file in a Windows form application

    string filename = "C:\\temp\example.xml";
    webBrowser1.Url = new Uri(filename);

  8. #8
    Join Date
    Mar 2003
    Posts
    145

    Re: Displaying an Xml file in a Windows form application

    Quote Originally Posted by dannystommen
    string filename = "C:\\temp\example.xml";
    webBrowser1.Url = new Uri(filename);
    It works.
    As I understand, using the Url property requires a file. But, is there a way to display the contents of a DataSet in an Xml format on the WebBrowser before the data is actually saved to an Xml file.

  9. #9
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: Displaying an Xml file in a Windows form application

    Quote Originally Posted by Holly_vi
    It works.
    As I understand, using the Url property requires a file. But, is there a way to display the contents of a DataSet in an Xml format on the WebBrowser before the data is actually saved to an Xml file.
    It does not neccesarally need a file, you could also fill in an url, try

    webBrowser1.Url = new Uri("http://www.codeguru.com");

    To get a dataset into Xml you should use the XmlDataDocument type instead of XmlDocument.
    The next codesnippet with a normal XmlDocument works
    Code:
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load("Properties.config");
    webBrowser1.Url = new Uri(xmlDoc.BaseURI);
    I never worked with datasets but it should be something like the next
    Code:
    XmlDataDocument xmlDataDoc = new XmlDataDocument(yourDataSet);
    webBrowser1.Url = new Uri(xmlDataDoc.BaseURI);

  10. #10
    Join Date
    Mar 2003
    Posts
    145

    Re: Displaying an Xml file in a Windows form application

    Quote Originally Posted by dannystommen
    I never worked with datasets but it should be something like the next
    Code:
    XmlDataDocument xmlDataDoc = new XmlDataDocument(yourDataSet);
    webBrowser1.Url = new Uri(xmlDataDoc.BaseURI);
    I need to display the contents of my DataSet before it is saved to file, so when I do as you suggest, xmlDataDoc.BaseURI returns null.
    Any suggestions?

  11. #11
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: Displaying an Xml file in a Windows form application

    I looked at it, and xmlDataDoc is indeed empty.

    I suggest to save the xmlDataDoc in a temp directory, then open the file with a xmlDocument, and after you did everything you wanted to do, you could delete the file

    Code:
             XmlDataDocument xmlDataDoc = new XmlDataDocument(dataset);
             string fileName = @"C:\temp\data.xml";
             xmlDataDoc.Save(fileName);
             XmlDocument xmlDoc = new XmlDocument();
             xmlDoc.Load(fileName);
             webBrowser1.Url = new Uri(xmlDoc.BaseURI);
    
             //do the things you want to do
    
             //at the end, delete the file
             FileInfo fi = new FileInfo(fileName);
             fi.Delete();

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured