Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Convert .aspx page to .xml

Posted on 2006-06-20
14
Medium Priority
?
2,134 Views
Last Modified: 2008-02-20
In C# how can I read a *.aspx page like this:

http://www.wilmerhale.com/antitrust_and_competition_rss.aspx

and have the data be written to an xml file with an .xml extension.

What I'm trying to do - is give users an option when using their rss readers of having the file as a pure .xml extensioned file or not.

I suppose also I should think about updates as if I can do this the .aspx file would get changed when a new item was added which would also change the .xml but for now I'd be happy if I could take that .aspx page and create an .xml file

 

0
Comment
Question by:dylanone
  • 7
  • 5
13 Comments
 
LVL 7

Expert Comment

by:mmarksbury
ID: 16946017
Use the System.Net.WebClient class to connect to and download the file programatically from C#.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16946129
Here is a class that I use:

using System.Net;
using System.IO;

public class WebPages
{

  public static string GetPageText(string url)
  {
    string htmlText = string.Empty;
           
    try
    {
      HttpWebRequest requestIP = (HttpWebRequest)WebRequest.Create(url);
      requestIP.Timeout = 10000;
      using (HttpWebResponse responseIP = (HttpWebResponse)requestIP.GetResponse())
      {
        using (Stream streamIP = responseIP.GetResponseStream())
        {
          using (StreamReader readerText = new StreamReader(streamIP))
          {
            htmlText = readerText.ReadToEnd();
          }
        }
      }
    }
    finally
    {
    }
    return htmlText;
  }
}
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16946141
Example usage:

using System.IO;

...

   string text = WebPages.GetPageText("http://www.wilmerhale.com/antitrust_and_competition_rss.aspx");

   StreamWriter writer = new StreamWriter(filePath);
   writer.Write(text);
   writer.Close();

Bob
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:dylanone
ID: 16951776
In looking at the code that was posted - I assume that filePath refers to a const value so I added that - I don't get an xml file written however - I don't get any errors in the code it just doesn't write the xml file:

Here is the aspx page where I'm calling the code behind:

<%@ Page language="c#" Codebehind="writexml.aspx.cs" AutoEventWireup="false" %>

Here is my code behind for right now I'm just testing:

using System.Net;
using System.IO;

public class writexml
{

  public static string GetPageText(string url)
  {
    string htmlText = string.Empty;
    const string FILE_NAME = "rss_test.xml";
           
    try
    {
      HttpWebRequest requestIP = (HttpWebRequest)WebRequest.Create(url);
      requestIP.Timeout = 10000;
      using (HttpWebResponse responseIP = (HttpWebResponse)requestIP.GetResponse())
      {
        using (Stream streamIP = responseIP.GetResponseStream())
        {
          using (StreamReader readerText = new StreamReader(streamIP))
          {
            htmlText = readerText.ReadToEnd();
                  string text = writexml.GetPageText("http://localhost/rss_test.aspx");

                   StreamWriter writer = new StreamWriter(FILE_NAME);
                    writer.Write(text);
                    writer.Close();
          }
        }
      }
    }
    finally
    {
    }
    return htmlText;
  }
}
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 400 total points
ID: 16953284
If you want to use it that way, try this:

using System.Net;
using System.IO;

public class WriteXml
{

  public static void SavePageText(string url, string fileName)
  {
    try
    {
     HttpWebRequest requestIP = (HttpWebRequest)WebRequest.Create(url);
      requestIP.Timeout = 10000;
      using (HttpWebResponse responseIP = (HttpWebResponse)requestIP.GetResponse())
      {
        using (Stream streamIP = responseIP.GetResponseStream())
        {
          using (StreamReader readerText = new StreamReader(streamIP))
          {
            string xmlText = readerText.ReadToEnd();

            using (StreamWriter writer = new StreamWriter(fileName));
            {
               writer.Write(xmlText);
            }
          }
        }
      }
    }
    finally
    {
    }
  }
}

From another method, call like this:
   WriteXml.SavePageText("http://www.wilmerhale.com/antitrust_and_competition_rss.aspx", "rss_test.xml");

Bob
0
 

Author Comment

by:dylanone
ID: 16996402
I get two errors with this code:

warning CS0642: Possible mistaken null statement
error S0246: The type or namespace name 'writer' could not be found (are you missing a using directive or an assembly reference?)

                                           The first error is inside this using line
                                           using (StreamWriter writer = new StreamWriter(fileName));
                                     {
             
                                           the second error is when
                                           this object is used which is defined in the line above it:
                                           writer.Write(xmlText);

using System.Net;
using System.IO;

public class WriteXml
{
      public static void SavePageText(string url, string fileName)
      {
            try
            {
                  HttpWebRequest requestIP = (HttpWebRequest)WebRequest.Create(url);
                  requestIP.Timeout = 10000;
                  using (HttpWebResponse responseIP = (HttpWebResponse)requestIP.GetResponse())
                  {
                        using (Stream streamIP = responseIP.GetResponseStream())
                        {
                              using (StreamReader readerText = new StreamReader(streamIP))
                              {
                                    string xmlText = readerText.ReadToEnd();

                                    using (StreamWriter writer = new StreamWriter(fileName));
                              {
                                    writer.Write(xmlText);
                              }
                              }
                        }
                  }
            }
            finally
            {
            }
      }
      public static void Main()
      {
            WriteXml.SavePageText("http://www.wilmerhale.com/antitrust_and_competition_rss.aspx", "rss_test.xml");
      }

}
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17127330
You have to put using statements at the top of the block:
using System.Net;
using System.IO;

Bob
0
 

Author Comment

by:dylanone
ID: 17127449
I have those two using statements as the first and second lines in the code - I don't believe I need to repeat them again in the block - correct?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17129214
This line:

  using (StreamWriter writer = new StreamWriter(fileName));

should be this:

   using (StreamWriter writer = new StreamWriter(fileName))

That should get rid of the second error.

Bob
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17129218
The way it looked above, I thought you had the using statements in the middle of the class, and they need to only be at the top of the module.

Bob
0
 

Author Comment

by:dylanone
ID: 17133501
I don't get any compile error now - however - the file doesn't get written - I tried
to even be specifc and try something like:

WriteXml.SavePageText("http://www.wilmerhale.com/antitrust_and_competition_rss.aspx", "c:\\temp\\rss_test.xml");

but no luck -



0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17133722
Is it making it to this line?

    writer.Write(xmlText);

Bob
0
 

Author Comment

by:dylanone
ID: 17287002
Thanks - I never did get this 100% working but I have a much better idea of the concept I need to do in order to build this one!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question