A personal repository of technical notes. - CSC

Xml.DocumentSource Will Not Load With Physical Path

Problem
ASP.NET Xml control will not load with physical path when XML file is not part of website. The following error is thrown:

Server Error System.Web.HttpException: 'c:\MyFolder\MyFile.xml' is a physical path.

Property System.Web.UI.WebControls.Xml.DocumentSource may not be a physical path to the requested XML file. It must be a virtual path that is a relative or root relative URL.

Solution
Tested on .NET Framework version 1.1.

1) Instantiate an XmlDocument using physical path.
2) Assign XmlDocument to Xml control

Example using a physical path:

<asp:xml id="xmlControl" transformsource="style.xslt" runat="server"></asp:xml>

...

protected System.Web.UI.WebControls.Xml xmlControl;

System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.Load("C:\MyFolder\MyFile.xml");

xmlControl.Document = xmlDoc;


Example using a website path:

<asp:xml id="xmlControl" transformsource="style.xslt" runat="server"></asp:xml>

...

protected System.Web.UI.WebControls.Xml xmlControl;

xmlControl.DocumentSource = "/MyWebSiteFolder/MyXml.xml";


References
"Xml Class." MSDN: Microsoft Development, MSDN Subscriptions, Resources, and More. 20 Feb. 2009 http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.xml(VS.71).aspx

"XmlDocument Class." MSDN: Microsoft Development, MSDN Subscriptions, Resources, and More. 20 Feb. 2009 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument(VS.71).aspx

No comments:

Post a Comment