I am trying to load an xml file from the media library but am having an issue with the pathing. I have been able to load the xml when the xml file is located in the actual server files, or when it is on another hosted site, but not when the file is in the media library. Does the xml file have to be a physical file hosted somewhere?
Here is my code for retrieving the path of the media item:
Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");
Sitecore.Data.Items.Item sampleItem = master.GetItem("/sitecore/media library/Files/eBooks/testxml");
Sitecore.Data.Items.Item sampleMedia = new Sitecore.Data.Items.MediaItem(sampleItem);
string url = Sitecore.StringUtil.EnsurePrefix('/', Sitecore.Resources.Media.MediaManager.GetMediaUrl(sampleMedia));
Then when I load the xml I am doing the following:
XmlDocument xDoc = new XmlDocument();
xDoc.Load(Server.MapPath(url));
The path it returns is correct, as I have tested putting it in an anchor tag to see if it would link to the xml file, and it does. I have found similar posts on this site but none seem to address media library items in the context of xml.Load.
Any information on if this is possible or what I can do to make it work would be much appreciated.
Thank you.
Use the stream of the media instead of trying to pass the path or using a WebClient:
Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");
Sitecore.Data.Items.Item sampleItem = master.GetItem("/sitecore/media library/Files/testxml");
Sitecore.Data.Items.Item sampleMedia = new Sitecore.Data.Items.MediaItem(sampleItem);
XmlDocument xmdDoc = new XmlDocument();
xmdDoc.Load(MediaManager.GetMedia(sampleMedia).GetStream().Stream);
Server.MapPath
will attempt to map a relative or virtual path to a physical file/folder on your server.
Try removing MapPath, and go strait for xDoc.Load(url);
. Alternatively, you could download the XML document using a WebClient and pass the string to your XmlDocument:
using (var client = new WebClient())
{
string strXml = client.DownloadString(new Uri(url));
xmlDoc.LoadXml(strXml);
}
Here is a sample code I used to read XML(or could be JSON) file from Sitecore Media Library.
Sitecore.Data.Items.Item xmlMedia = new Sitecore.Data.Items.MediaItem(Sitecore.Context.Database.GetItem("/~/media/mySiteName/files/sampleXmlItem"));
//OR
Sitecore.Data.Items.Item xmlMedia = new Sitecore.Data.Items.MediaItem(Sitecore.Context.Database.GetItem(RenderingContext.Current.Rendering.DataSource));
var fileType = xmlMedia.Fields["Extension"].Value;
if (fileType == "xml" || fileType == "XML")
{
XmlDocument xmdDoc = new XmlDocument();
xmdDoc.Load(Sitecore.Resources.Media.MediaManager.GetMedia(xmlMedia).GetStream().Stream);
XmlDocument innerXmdDoc = new XmlDocument();
innerXmdDoc.LoadXml(xmdDoc.LastChild.OuterXml);
myData = JsonConvert.SerializeXmlNode(innerXmdDoc);
}
Instead of using the hardcoded item path, you can use the xml file as the datasource and read the xml file by reading current rendering item.
You can go through the below source for the complete implementation.
Source: Reading XML/JSON from Media library in Sitecore MVC
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With