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
Solved

Reading Xml

Posted on 2004-10-19
5
311 Views
Last Modified: 2013-11-19
I have a page to display xml from an online file.

The HTML part is as follows

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML>
      <HEAD>
            <title>CodeCube RSS Reader</title>
<?xml version="1.0" encoding="iso-8859-1" ?>

            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
            <link href="../../global/master.css" rel="stylesheet" type="text/css">
      </HEAD>
      <body>
            <form id="form1" runat="server">
                  <p>
                        RSS URI:
                        <asp:textbox id="uri" columns="50" runat="server" />
                        <asp:button ID="submit" Text="Get Feed" runat="server" />
                  </p>
                  <h1>
                        <asp:hyperlink ID="lblTitle" runat="server" />
                  </h1>
                  <p>
                        <asp:label ID="lblDescription" runat="server" />
                  </p>
                  <p>
                        <asp:label ID="result" runat="server" />
                  </p>
                  <hr>
                  <asp:label ID="errmsg" runat="server" />
            </form>
      </body>
</HTML>


The Code to load the xml is

private void submit_Click(object sender, System.EventArgs e)
            {
                  string url = uri.Text;
                  //XmlDocument xml = new XmlDocument();
                  //XmlTextReader reader = new XmlTextReader(url);
                  //xml.Load(url);
                  
                  XmlTextReader reader = new XmlTextReader(url);
     
                  reader.WhitespaceHandling = WhitespaceHandling.None;
                  XmlDocument doc = new XmlDocument();
                  //Load the file into the XmlDocument
                  doc.Load(reader);
                  //Close off the connection to the file.
                  reader.Close();
                  
                  
                  


                  XmlNodeList nodes = doc.SelectNodes("//item");

                  StringBuilder str = new StringBuilder();
                  str.Append("<table>");
                  foreach(XmlNode item in nodes)
                  {
                        foreach(XmlNode child in item.ChildNodes)
                        {
                              str.Append("<tr><th>");
                              str.Append(child.Name);
                              str.Append("</th><td>");
                              str.Append(child.InnerText);
                              str.Append("</td></tr>");
                        }
                        str.Append("<tr><td>&nbsp;</td></tr>");
                  }
                  str.Append("</table>");
                  result.Text = str.ToString();
                  errmsg.Text = "";
            }


I Have tried many different alternatives but cant figure out why it wont load, when i try with this link http://www.microsite.reuters.co.uk/rss/uk/topNews

it says

The underlying connection was closed: The remote name could not be resolved.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.WebException: The underlying connection was closed: The remote name could not be resolved.

Source Error:


Line 151:                  XmlDocument doc = new XmlDocument();
Line 152:                  //Load the file into the XmlDocument
Line 153:                  doc.Load(reader);
Line 154:                  //Close off the connection to the file.
Line 155:                  reader.Close();
 


Can anyone help me to get this working with the site link i gave above.

Thanks
0
Comment
Question by:ab0u110a
5 Comments
 
LVL 28

Expert Comment

by:mmarinov
ID: 12345841
Hi ab0u110a,

you have to use in this way:

    //Create the XmlDocument.
    XmlDocument doc = new XmlDocument();

    //Load the the document with the last book node.
    XmlTextReader reader = new XmlTextReader(url);
    reader.WhitespaceHandling = WhitespaceHandling.None;
    reader.MoveToContent();
    reader.Read();
    doc.Load(reader);



Regards!
B..M
0
 
LVL 22

Accepted Solution

by:
CJ_S earned 445 total points
ID: 12345842
The XmlDocument load methoid should work correctly. Do note that the url value should contain a valid path to an XML document. Which is probably not the case with your XML document.

private void submit_Click(object sender, System.EventArgs e)
          {
               string url = uri.Text;
               XmlDocument xml = new XmlDocument();
               xml.Load(url);
               
               XmlNodeList nodes = doc.SelectNodes("//item");

               StringBuilder str = new StringBuilder();
               str.Append("<table>");
               foreach(XmlNode item in nodes)
               {
                    foreach(XmlNode child in item.ChildNodes)
                    {
                         str.Append("<tr><th>");
                         str.Append(child.Name);
                         str.Append("</th><td>");
                         str.Append(child.InnerText);
                         str.Append("</td></tr>");
                    }
                    str.Append("<tr><td>&nbsp;</td></tr>");
               }
               str.Append("</table>");
               result.Text = str.ToString();
               errmsg.Text = "";
          }


Please note
0
 
LVL 7

Expert Comment

by:sukumar_diya
ID: 12345885
Hi pal,
I have cheked the code u posted in this question it is working fine. I used the same URL. I have pasted the result.
I have also pasted the code in my form(It is the same you posted here...) Any way check this also....

Cheers
Sukumar

Result :
++++++
title Abu Hamza accused of inciting murder
guid 605525
link http://www.reuters.co.uk/newsPackageArticle.jhtml?type=topNews&storyID=605525&src=rss/uk/topNews§ion=news 
pubDate Tue, 19 Oct 2004 10:49:28 GMT  
description LONDON (Reuters) - Abu Hamza al-Masri, the radical muslim cleric wanted by the United States, has been charged by police with 16 counts including inciting murder and racial hatred.
 
title Myanmar PM ousted and arrested
guid 605479
link http://www.reuters.co.uk/newsPackageArticle.jhtml?type=topNews&storyID=605479&src=rss/uk/topNews§ion=news 
pubDate Tue, 19 Oct 2004 09:52:01 GMT  
description BANGKOK (Reuters) - Myanmar's prime minister has been ousted and put under house arrest after a lengthy power struggle that has tightened the grip of hardliners in the military government, Thai officials and diplomats say.
 
title Government rolls the dice on gambling bill
guid 605420
link http://www.reuters.co.uk/newsPackageArticle.jhtml?type=topNews&storyID=605420&src=rss/uk/topNews§ion=news 
pubDate Tue, 19 Oct 2004 09:11:41 GMT  
description LONDON (Reuters) - An overhaul of 40-year-old gambling laws moves a step closer today when the government presents a long-awaited bill to parliament.
 
title Shake-up at Sainsbury to boost sales
guid 605423
link http://www.reuters.co.uk/newsPackageArticle.jhtml?type=topNews&storyID=605423&src=rss/uk/topNews§ion=news 
pubDate Tue, 19 Oct 2004 09:13:08 GMT  
description LONDON (Reuters) - In a wide-ranging business revamp, Sainsbury, Britain's third-biggest supermarket chain, says it will axe 750 head-office jobs, hire 3,000 front-line staff and slash prices to compete with market leaders Tesco and Wal-Mart's Asda.
 
title Four more arrested over teen's murder
guid 605143
link http://www.reuters.co.uk/newsPackageArticle.jhtml?type=topNews&storyID=605143&src=rss/uk/topNews§ion=news 
pubDate Tue, 19 Oct 2004 03:05:24 GMT  
description LONDON (Reuters) - Police say they have arrested four more people over the death of 14-year-old Danielle Beccan, who was shot and killed as she walked home last week from a funfair in Nottingham.
 
title PFA boss says Mutu admits positive test
guid 605450
link http://www.reuters.co.uk/newsPackageArticle.jhtml?type=topNews&storyID=605450&src=rss/uk/topNews§ion=news 
pubDate Tue, 19 Oct 2004 09:27:50 GMT  
description LONDON/BUCHAREST (Reuters) - Chelsea striker Adrian Mutu has admitted testing positive for cocaine and will not ask for his 'B' sample to be analysed, players' union chief Gordon Taylor has said.
 
title Iraq war increases terrorism risk -report
guid 605464
link http://www.reuters.co.uk/newsPackageArticle.jhtml?type=topNews&storyID=605464&src=rss/uk/topNews§ion=news 
pubDate Tue, 19 Oct 2004 09:34:02 GMT  
description LONDON (Reuters) - War in Iraq has increased the risk of terrorism against the West, at least for the short term, the International Institute for Strategic Studies has said in its annual report on the world's militaries.
 
title BBC told to sell access to archives
guid 605506
link http://www.reuters.co.uk/newsPackageArticle.jhtml?type=topNews&storyID=605506&src=rss/uk/topNews§ion=news 
pubDate Tue, 19 Oct 2004 10:34:09 GMT  
description LONDON (Reuters) - The BBC has helped drive the take-up of digital radio but should consider making programmes from its vast radio archives available to private sector companies, a government commission says.
 
title Cheap child vaccine developed
guid 605166
link http://www.reuters.co.uk/newsPackageArticle.jhtml?type=topNews&storyID=605166&src=rss/uk/topNews§ion=news 
pubDate Tue, 19 Oct 2004 05:30:02 GMT  
description LONDON (Reuters) - British scientists have developed a new way of storing childhood vaccines without refrigeration in a move that could slash costs and increase access to life-savings shots in poor countries.
 
title "Chavs" sexing up the language
guid 605341
link http://www.reuters.co.uk/newsPackageArticle.jhtml?type=topNews&storyID=605341&src=rss/uk/topNews§ion=news 
pubDate Tue, 19 Oct 2004 07:56:02 GMT  
description LONDON (Reuters) - Last year it was "sexing up;" in 1993 Britons were "having it large" and in 1980 it was all about "power dressing."

Codebehind
+++++++++

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Text;
namespace DRDa
{
      /// <summary>
      /// Summary description for WebForm1.
      /// </summary>
      public class WebForm1 : System.Web.UI.Page
      {
            protected System.Web.UI.WebControls.TextBox uri;
            protected System.Web.UI.WebControls.Button submit;
            protected System.Web.UI.WebControls.HyperLink lblTitle;
            protected System.Web.UI.WebControls.Label lblDescription;
            protected System.Web.UI.WebControls.Label result;
            protected System.Web.UI.WebControls.Label errmsg;

            private void Page_Load(object sender, System.EventArgs e)
            {
                  
            }
            private void submit_Click(object sender, System.EventArgs e)
            {
                  string url = uri.Text;
                  //XmlDocument xml = new XmlDocument();
                  //XmlTextReader reader = new XmlTextReader(url);
                  //xml.Load(url);
               
                  XmlTextReader reader = new XmlTextReader(url);
     
                  reader.WhitespaceHandling = WhitespaceHandling.None;
                  XmlDocument doc = new XmlDocument();
                  //Load the file into the XmlDocument
                  doc.Load(reader);
                  //Close off the connection to the file.
                  reader.Close();
               
               
               


                  XmlNodeList nodes = doc.SelectNodes("//item");

                  StringBuilder str = new StringBuilder();
                  str.Append("<table>");
                  foreach(XmlNode item in nodes)
                  {
                        foreach(XmlNode child in item.ChildNodes)
                        {
                              str.Append("<tr><th>");
                              str.Append(child.Name);
                              str.Append("</th><td>");
                              str.Append(child.InnerText);
                              str.Append("</td></tr>");
                        }
                        str.Append("<tr><td>&nbsp;</td></tr>");
                  }
                  str.Append("</table>");
                  result.Text = str.ToString();
                  errmsg.Text = "";
            }


            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                  //
                  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                  //
                  InitializeComponent();
                  base.OnInit(e);
            }
            
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {    
                  this.submit.Click += new System.EventHandler(this.submit_Click);
                  this.Load += new System.EventHandler(this.Page_Load);

            }
            #endregion
      }
}


HTML CODE
+++++++++

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="DRDa.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML>
  <HEAD>
          <title>CodeCube RSS Reader</title>
<?xml version="1.0" encoding="iso-8859-1" ?>

          <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
          <link href="../../global/master.css" rel="stylesheet" type="text/css">
  </HEAD>
     <body>
          <form id="form1" runat="server">
               <p>
                    RSS URI:
                    <asp:textbox id="uri" columns="50" runat="server" />
                    <asp:button ID="submit" Text="Get Feed" runat="server" />
               </p>
               <h1>
                    <asp:hyperlink ID="lblTitle" runat="server" />
               </h1>
               <p>
                    <asp:label ID="lblDescription" runat="server" />
               </p>
               <p>
                    <asp:label ID="result" runat="server" />
               </p>
               <hr>
               <asp:label ID="errmsg" runat="server" />
          </form>
     </body>
</HTML>


0
 

Author Comment

by:ab0u110a
ID: 12345916
Thanks guys
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 12346259
ab0u110a,
Please accept one of the comments as the accepted answer.

Thanks in advance.

CJ.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
The viewer will learn how to count occurrences of each item in an array.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

809 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