Solved

Convert .aspx page to .xml

Posted on 2006-06-20
14
1,784 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
14 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
 

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 100 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now