Solved

Convert .aspx page to .xml

Posted on 2006-06-20
14
2,028 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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
 
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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

623 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