Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sitecore Loading an XML File from Media Library?

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.

like image 386
Paul Matos Avatar asked Aug 22 '12 18:08

Paul Matos


3 Answers

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);
like image 89
Marek Musielak Avatar answered Sep 21 '22 03:09

Marek Musielak


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);
}
like image 33
Derek Hunziker Avatar answered Sep 23 '22 03:09

Derek Hunziker


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

like image 29
Pranay Avatar answered Sep 21 '22 03:09

Pranay