Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Convert .aspx page to .xml

Posted on 2006-06-20
14
Medium Priority
?
2,086 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

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

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…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

715 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