Transforming XML and display as a label in ASPX page

M

Martin Honnen

Can I get XML data from an external XML file loaded in an ASPX page to be
displayed as a "Label"?
I don't want to use the XML control <asp:xml>, but a label in order to
display the source.

Here comes the example XML file:
http://webservices.amazon.de/onca/x...kup&ItemId=B0001BXYQ0&ResponseGroup=SalesRank

I want to put the SalesRank value into my label.

Here is an example using XPathDocument to read out the SalesRank:

<%@ Page Language="C#" Debug="True" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.XPath" %>
<script runat="server">
void Page_Load () {
XPathDocument amazonResult = new
XPathDocument(@"http://webservices.amazon.de/onca/x...kup&ItemId=B0001BXYQ0&ResponseGroup=SalesRank");
XPathNavigator xpathNavigator = amazonResult.CreateNavigator();
XPathExpression xpathExpression =
xpathNavigator.Compile(@"/am:ItemLookupResponse/am:Items/am:Item/am:SalesRank/text()");
XmlNamespaceManager namespaceManager = new
XmlNamespaceManager(xpathNavigator.NameTable);
namespaceManager.AddNamespace("am",
"http://xml.amazon.com/AWSECommerceService/2004-08-01");
xpathExpression.SetContext(namespaceManager);
XPathNodeIterator nodeIterator = xpathNavigator.Select(xpathExpression);
if (nodeIterator.MoveNext()) {
Label1.Text = nodeIterator.Current.Value;
}
}
</script>
<form runat="server">
<asp:Label runat="server" id="Label1" />
</form>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top