Sitecore Loading an XML File from Media Library?
Asked Answered
S

3

5

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.

Snowblind answered 22/8, 2012 at 18:46 Comment(0)
D
6

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);
Drumlin answered 22/8, 2012 at 20:16 Comment(0)
K
6

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);
}
Kosel answered 22/8, 2012 at 19:16 Comment(1)
Using HTTP (Web Client) for this, doesn't seem appropriate to me. The file may not be on disk, but it can be read & streamed from the database. So using a stream like in the answer from Maras Musielak seems better.Tigress
D
6

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);
Drumlin answered 22/8, 2012 at 20:16 Comment(0)
S
1

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

Sunbonnet answered 23/12, 2015 at 17:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.