0

So I am using the below code to do a xslt transformation. But I am pretty new to ASP.NET development so the errors are slightly misleading. This code is generating an error that states it doesn't understand physical paths and only virtual paths. What is a virtual path and how do I make one from a specified physical path?

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="ViewerASP.SiteMaster" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<%@ Import Namespace="System.Xml.XPath" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head runat="server">
    <title>Viewer</title>
    <link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
    <script language="C#" runat="server">
        public void Page_Load(Object sender, EventArgs E) {
            string xmlPath = Server.MapPath("physicaladdresshere");
            string xslPath = Server.MapPath("physicaladdresshere");
            //Instantiate the XPathDocument Class
            XPathDocument doc = new XPathDocument(xmlPath);
            //INstantiate the XslTransform Class
            XslTransform transform = new XslTransform();
            transform.Load(xslPath);
            //Custom format the indenting of the output document using XmlTextWriter
            XmlTextWriter writer = new XmlTextWriter(Response.Output);
            writer.Formatting = Formatting.Indented;
            writer.Indentation=4;
            transform.Transform(doc,null,writer);
        }
    </script>
    <asp:ContentPlaceHolder ID="HeadContent" runat="server">
    </asp:ContentPlaceHolder>
</head>
        <div class="main">
            <asp:ContentPlaceHolder ID="MainContent" runat="server"/>
        </div>
</body>
</html>
2
  • How does your physicaladdresshere look like? Is it relative or absolute? If relative - to what directory? Commented May 22, 2014 at 16:51
  • Just as an example it would look something like this: C:\Users\kyle\Desktop\file.xml. I tried hardcoding this and it gave me errors about invalid escape character sequence so I changed it to this: C:\\Users\\kyle\\Desktop\\file.xml and received the error original posted about. Commented May 22, 2014 at 16:57

1 Answer 1

1

Server.MapPath maps virtual path - that is path in a virtual IIS directory - to the physical path on a server. Apparently you do not need that, since you already have an absolute server-side path. Just remove these Server.MapPath calls:

string xmlPath = "C:\\Users\\kyle\\Desktop\\file.xml";
...
Sign up to request clarification or add additional context in comments.

5 Comments

Ok I think I follow that. But would this work if I have a web form where the user browses to their xml file? Can I retrieve their local path from that. I'm trying to do my transformation client side.
Oh, than I am afraid that will not work at all. You cannot access file system of the end user - that would be a huge security breach. Also note that C# code is something that is run on the server, not on the remote user computer. The best you can do is give users a file upload page, where they upload files and you run transformations on the server-side
Oh alright I understand. There is a asp server then can run locally though right? Under add/remove programs/features? Is there anyway to just run the code through the c#? So we would have to create a web server that accepts the uploaded file and then provides that url back?
@Kyle, you would need a web server anyway - this is how web applications work. And yes, the actual process might be: 1 - users navigate to the page, upload files; 2 - server runs the transformation; 3 - users receive a link to download new file, or maybe just get a file back in a response right away. THat is just an option though - i have no idea what you app is about
Basically the whole idea is to take an xml file, apply the xsl tranform and render that page back. I've successfully done it python, java and visual c#. But, we are trying to make something cross platform that can run in a browser.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.