Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Istvan Pszota 59 posts 191 karma points
    Oct 08, 2014 @ 21:40
    Istvan Pszota
    0

    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)
    

    What i'm doing wrong?

    Any ideas would be highly appreciated!

    Cheers, Istvan

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Oct 08, 2014 @ 21:50
    Chriztian Steinmeier
    0

    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

  • Istvan Pszota 59 posts 191 karma points
    Oct 08, 2014 @ 22:05
    Istvan Pszota
    0

    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

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Oct 08, 2014 @ 22:22
    Chriztian Steinmeier
    0

    Hi again,

    Just to check something, could you try using the native document() function, e.g.:

    <xsl:variable name="cart" select="document('http://localhost:18944/umbraco/webshop/webshop/GetCartContent?sid=19ce2b1c-99f0-4621-86b2-a728870f11ec')"/>
    

    It doesn't have the cache option that GetXmlDocumentByUrl() has but should be able to get the document, nevertheless...

    /Chriztian

  • Istvan Pszota 59 posts 191 karma points
    Oct 08, 2014 @ 22:27
    Istvan Pszota
    0

    I'm receiving the following error msg now:

    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)
    
  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Oct 08, 2014 @ 22:39
    Chriztian Steinmeier
    0

    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:

    <xsl:variable name="cart" select="umbraco.library:GetXmlDocumentByUrl('http://greystate.dk/umbraco-rss.xml')"/>
    

    ?

    /Chriztian

  • Istvan Pszota 59 posts 191 karma points
    Oct 08, 2014 @ 22:47
    Istvan Pszota
    0

    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:

    [{"sid":"19ce2b1c-99f0-4621-86b2-a728870f11ec","productId":1049,"quantity":5,"netUnitPrice":850,"title":"Title 1","description":"Description 1","addedTimestamp":"2014-09-29T07:59:52.18"},{"sid":"19ce2b1c-99f0-4621-86b2-a728870f11ec","productId":1050,"quantity":15,"netUnitPrice":2550,"title":"Title 2","description":"Description 2","addedTimestamp":"2014-09-29T08:00:06.21"}]
    

    Is this something that makes the problem?

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Oct 08, 2014 @ 22:56
    Chriztian Steinmeier
    100

    Hi Istvan,

    Is this something that makes the problem?

    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

  • Istvan Pszota 59 posts 191 karma points
    Oct 08, 2014 @ 22:58
    Istvan Pszota
    0

    Great, at least we are nearby.. :)

    My code that is generating the output is still:

        [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>();
            }
       }
    
  • Istvan Pszota 59 posts 191 karma points
    Oct 09, 2014 @ 14:04
    Istvan Pszota
    0

    Hi!

    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();
    
            }
    

    Thanks for pointing out the basic error!

    Regards, Istvan

Please Sign in or register to post replies

Write your reply to:

Draft