Generate XML to process through GetXmlDocumentByUrl()
Hello!
I want to generate XML content from Web API to read and proces in an XSLT file, using umbraco.library:GetXmlDocumentByUrl().
I have managed to generate the XML file, and it is returned upon request, the content is displayed in web browser, but i receive an error message, when trying to read it with umbraco.library:GetXmlDocumentByUrl!
I'm generating the XML content like:
[System.Web.Http.HttpGet]
[ScriptMethod(ResponseFormat=ResponseFormat.Xml)]
public List<webshopCart> GetCartContent(string sid)
{
var cartItems = from c in dc.webshopCarts where c.sid == Guid.Parse(sid) select c;
return cartItems.ToList<webshopCart>();
}
This displays the following result in a web browser:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<ArrayOfwebshopCart xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/WebshopPackage">
<webshopCart>
<addedTimestamp>2014-09-29T07:59:52.18</addedTimestamp>
<description>Description 1</description>
<netUnitPrice>850</netUnitPrice>
<productId>1049</productId>
<quantity>5</quantity>
<sid>19ce2b1c-99f0-4621-86b2-a728870f11ec</sid>
<title>Product 1</title>
</webshopCart>
<webshopCart>
<addedTimestamp>2014-09-29T08:00:06.21</addedTimestamp>
<description>Description 2</description>
<netUnitPrice>2550</netUnitPrice>
<productId>1050</productId>
<quantity>15</quantity>
<sid>19ce2b1c-99f0-4621-86b2-a728870f11ec</sid>
<title>Product 2</title>
</webshopCart>
</ArrayOfwebshopCart>
My XSL file that used to read this:
<xsl:variable name="cart" select="umbraco.library:GetXmlDocumentByUrl('http://localhost:18944/umbraco/webshop/webshop/GetCartContent?sid=19ce2b1c-99f0-4621-86b2-a728870f11ec')"/>
<!--This value-of is only to display the XML content that was read in-->
<xsl:value-of select="$cart" />
<ul>
<xsl:for-each select="$cart/tmp:ArrayOfwebshopCart/tmp:webshopCart">
<li><xsl:value-of select="tmp:title" />
</li>
</xsl:for-each>
</ul>
And the error message i receive:
System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1. at System.Xml.XmlTextReaderImpl.Throw(Exception e) at System.Xml.XmlTextReaderImpl.Throw(String res, String arg) at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace() at System.Xml.XmlTextReaderImpl.ParseDocumentContent() at System.Xml.XmlTextReaderImpl.Read() at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace) at System.Xml.XmlDocument.Load(XmlReader reader) at umbraco.library.GetXmlDocumentByUrl(String Url)
First, try loading the URL into an HTTP Client app or similar, to see what's actually returned from the server (e.g., MIME-Type, encoding etc.)
Also, make sure that there's no whitespace before the initial < character, which could also make it choke.
Finally, remember that the XSLT processor will be making the request, which sometimes means that you have to configure ports or similar if working on a local machine (usually not, though).
The error you're getting suggests to me that the processor is either getting an error document (403, 404 or 5xx) - or that the XML is invalid (e.g. if there's any leading whitespace etc.)
Checked with Fiddler, but there is no whitespace efore the initial <.
Content-Type: application/xml; charset=utf-8
I have entered the full URL, with port.
If i modify anyting in the URL provided to the function, i receive an error document message (500, for example!)
The XML is generated with .List(), i have not modified anything else..
Error parsing the XSLT:
System.Xml.Xsl.XslTransformException: An error occurred while loading document 'http://localhost:18944/umbraco/webshop/webshop/GetCartContent?sid=19ce2b1c-99f0-4621-86b2-a728870f11ec'. See InnerException for a complete description of the error. ---> System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1. at System.Xml.XmlTextReaderImpl.Throw(Exception e) at System.Xml.XmlTextReaderImpl.Throw(String res, String arg) at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace() at System.Xml.XmlTextReaderImpl.ParseDocumentContent() at System.Xml.XmlTextReaderImpl.Read() at System.Xml.XPath.XPathDocument.LoadFromReader(XmlReader reader, XmlSpace space) at System.Xml.XPath.XPathDocument..ctor(XmlReader reader, XmlSpace space) at System.Xml.Xsl.Runtime.XmlQueryContext.ConstructDocument(Object dataSource, String uriRelative, Uri uriResolved) at System.Xml.Xsl.Runtime.XmlQueryContext.GetDataSource(String uriRelative, String uriBase) --- End of inner exception stack trace --- at System.Xml.Xsl.Runtime.XmlQueryContext.GetDataSource(String uriRelative, String uriBase) at (XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime) at Root(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime) at Execute(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime) at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlSequenceWriter results) at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter writer) at System.Xml.Xsl.XslCompiledTransform.Transform(IXPathNavigable input, XsltArgumentList arguments, XmlWriter results, XmlResolver documentResolver) at System.Xml.Xsl.XslCompiledTransform.Transform(IXPathNavigable input, XsltArgumentList arguments, TextWriter results) at umbraco.macro.GetXsltTransformResult(XmlDocument macroXml, XslCompiledTransform xslt, Dictionary`2 parameters) at umbraco.presentation.umbraco.developer.Xslt.xsltVisualize.visualizeDo_Click(Object sender, EventArgs e)
Well, then I'm pretty sure it's something to do with the response being an error document... that URL is probably returning something different when called from the XSLT process.
Do you get the correct response if you open the URL from a command prompt?
Just trying to think if the XSLT processor has less permissions etc. - can you open a public URL (e.g. an RSS feed) from the XSLT - i.e. like this:
You're getting JSON back instead of XML... but your C# code seems to indicate that you want to return XML, right? Or did you change that at some point?
[PluginController("Webshop")]
public class WebshopController : UmbracoApiController
{
datasetDataContext dc = new datasetDataContext();
dataClasses dClasses = new dataClasses();
[System.Web.Http.HttpGet]
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
public List<webshopCart> GetCartContent(string sid)
{
var cartItems = from c in dc.webshopCarts where c.sid == Guid.Parse(sid) select c;
return cartItems.ToList<webshopCart>();
}
}
After a couple of Google search i have ended up with the below working solution:
[System.Web.Http.HttpGet]
[ScriptMethod(ResponseFormat = ResponseFormat.Xml, XmlSerializeString = true,UseHttpGet=true)]
public HttpResponseMessage GetCartContent(string sid)
{
var cartItems = from c in dc.webshopCarts where c.sid == Guid.Parse(sid) select c;
HttpContext.Current.Response.ContentType = "application/xml";
HttpContext.Current.Response.Write("<Cart><Item><title>Hello World</title></Item></Cart>");
return new HttpResponseMessage();
}
Generate XML to process through GetXmlDocumentByUrl()
Hello!
I want to generate XML content from Web API to read and proces in an XSLT file, using umbraco.library:GetXmlDocumentByUrl().
I have managed to generate the XML file, and it is returned upon request, the content is displayed in web browser, but i receive an error message, when trying to read it with umbraco.library:GetXmlDocumentByUrl!
I'm generating the XML content like:
This displays the following result in a web browser:
My XSL file that used to read this:
And the error message i receive:
What i'm doing wrong?
Any ideas would be highly appreciated!
Cheers, Istvan
Hi Istvan,
First, try loading the URL into an HTTP Client app or similar, to see what's actually returned from the server (e.g., MIME-Type, encoding etc.)
Also, make sure that there's no whitespace before the initial
<
character, which could also make it choke.Finally, remember that the XSLT processor will be making the request, which sometimes means that you have to configure ports or similar if working on a local machine (usually not, though).
The error you're getting suggests to me that the processor is either getting an error document (403, 404 or 5xx) - or that the XML is invalid (e.g. if there's any leading whitespace etc.)
/Chriztian
Hi Chriztian!
Checked with Fiddler, but there is no whitespace efore the initial <. Content-Type: application/xml; charset=utf-8
I have entered the full URL, with port. If i modify anyting in the URL provided to the function, i receive an error document message (500, for example!)
The XML is generated with .List(), i have not modified anything else..
Thanks, Istvan
Hi again,
Just to check something, could you try using the native document() function, e.g.:
It doesn't have the cache option that GetXmlDocumentByUrl() has but should be able to get the document, nevertheless...
/Chriztian
I'm receiving the following error msg now:
Well, then I'm pretty sure it's something to do with the response being an error document... that URL is probably returning something different when called from the XSLT process.
Do you get the correct response if you open the URL from a command prompt?
Just trying to think if the XSLT processor has less permissions etc. - can you open a public URL (e.g. an RSS feed) from the XSLT - i.e. like this:
?
/Chriztian
I have tried to open a public RSS feed, it is loaded without any problem.
But, when i opened my URL from a command prompt, the result was a JSON file, with JSON formatting:
Is this something that makes the problem?
Hi Istvan,
Yes - that's exactly the problem :)
You're getting JSON back instead of XML... but your C# code seems to indicate that you want to return XML, right? Or did you change that at some point?
/Chriztian
Great, at least we are nearby.. :)
My code that is generating the output is still:
Hi!
After a couple of Google search i have ended up with the below working solution:
Thanks for pointing out the basic error!
Regards, Istvan
is working on a reply...