Xml Control ASP 2.0

  • Thread starter Thread starter saywhat
  • Start date Start date
S

saywhat

For some reason, when I reference an external document using the
DocumentSource property using the web control, I get an error stating
"http://www.somesite.com/some.xml is not a valid virtual path".

<asp:XmlID="Xml1"runat="server"DocumentSource="http://www.somesite.com/some.xml"></asp:Xml>



It is a valid address, in IE the xml page loads right up, so I know it's a
VS problem and not the server.

Am I using incorrec syntax to specify the URL? No info that I can find on
the web, though many postings from other folks who have same problem.
 
saywhat,
No, you aren't using incorrect syntax, but you aren't reading the exception
message correctly either. a remote url with the HTTP:// protocol moniker is
not a "Virtual Path".

Something like /myxml.xml is a virtual path.
Peter
 
So then how can I do this?

I think what Peter means is you are suppose to use a file path, not a
URL. Here is what might work.

protected void Button1_Click(object sender, EventArgs e)
{
string lcUrl = "http://peterkellner.net/?feed=rss";

HttpWebRequest loHttp =
(HttpWebRequest) WebRequest.Create(lcUrl);

HttpWebResponse loWebResponse =
(HttpWebResponse)loHttp.GetResponse();

Encoding enc = Encoding.GetEncoding(1252); // Windows default
Code Page

StreamReader loResponseStream =
new StreamReader(loWebResponse.GetResponseStream(), enc);

string lcHtml = loResponseStream.ReadToEnd();

loWebResponse.Close();
loResponseStream.Close();

Xml1.DocumentContent = lcHtml;




}
Peter Kellner
http://peterkellner.net
 
WHEW... why is it that difficult? Is there not an easier way to do this
with the standard xml control? Geeeez...


It worked BTW :-D
 
WHEW... why is it that difficult? Is there not an easier way to do this
with the standard xml control? Geeeez...


It worked BTW :-D

Actually, I meant to say there is probably an easier way to do it but
I just don't know it. I'm so use to bending streamers and readers
that it's getting second nature. I do like the flexablity though.

If you find an easier way, let me know.
Peter Kellner
http://peterkellner.net
 
You can override the default behavior by adding a DocumentUrl Property and
making it "Get" the http - based xml as Peter K suggested. here is an example:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Xml;

namespace PAB.WebControls
{
[DefaultProperty("DocumentUrl")]
[ToolboxData("<{0}:CustomXml1 runat=server></{0}:CustomXml1>")]
public class CustomXml : System.Web.UI.WebControls.Xml
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
private string documentUrl;
public string DocumentUrl
{
get
{
return documentUrl;
}

set
{
documentUrl = value;
WebRequest req = WebRequest.Create(documentUrl);
WebResponse resp = req.GetResponse();
XmlTextReader reader = new
XmlTextReader(resp.GetResponseStream());
this.Document = new XmlDocument();
this.Document.Load(reader);
}
}

}
}

// note that the Document property is deprecated in ASP.NET 2.0 but it still
works fine. Later on you may wish to conform by using an XPathNavigator.
Peter




--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com
 
You can override the default behavior by adding a DocumentUrl Property and
making it "Get" the http - based xml as Peter K suggested. here is an example:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Xml;

namespace PAB.WebControls
{
[DefaultProperty("DocumentUrl")]
[ToolboxData("<{0}:CustomXml1 runat=server></{0}:CustomXml1>")]
public class CustomXml : System.Web.UI.WebControls.Xml
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
private string documentUrl;
public string DocumentUrl
{
get
{
return documentUrl;
}

set
{
documentUrl = value;
WebRequest req = WebRequest.Create(documentUrl);
WebResponse resp = req.GetResponse();
XmlTextReader reader = new
XmlTextReader(resp.GetResponseStream());
this.Document = new XmlDocument();
this.Document.Load(reader);
}
}

}
}

// note that the Document property is deprecated in ASP.NET 2.0 but it still
works fine. Later on you may wish to conform by using an XPathNavigator.
Peter

very nice! thanks.
Peter Kellner
http://peterkellner.net
 
Peter, thanks for "making me think", your stuff is great. Here's the "full
banana":

http://www.eggheadcafe.com/articles/20060603.asp

Cheers,

Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




PeterKellner said:
You can override the default behavior by adding a DocumentUrl Property and
making it "Get" the http - based xml as Peter K suggested. here is an example:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Xml;

namespace PAB.WebControls
{
[DefaultProperty("DocumentUrl")]
[ToolboxData("<{0}:CustomXml1 runat=server></{0}:CustomXml1>")]
public class CustomXml : System.Web.UI.WebControls.Xml
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
private string documentUrl;
public string DocumentUrl
{
get
{
return documentUrl;
}

set
{
documentUrl = value;
WebRequest req = WebRequest.Create(documentUrl);
WebResponse resp = req.GetResponse();
XmlTextReader reader = new
XmlTextReader(resp.GetResponseStream());
this.Document = new XmlDocument();
this.Document.Load(reader);
}
}

}
}

// note that the Document property is deprecated in ASP.NET 2.0 but it still
works fine. Later on you may wish to conform by using an XPathNavigator.
Peter

very nice! thanks.
Peter Kellner
http://peterkellner.net
 
Back
Top