Solved

Convert .aspx page to .xml

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

 

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

Independent Software Vendors: 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

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 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