Sitecore Loading an XML file from a media library?

I am trying to download an xml file from a media library, but I have a problem with the outline. I was able to download xml when the XML file is in the actual server files, or when it is on another hosted site, but not when the file is in the library. Should the xml file be a physical file hosted somewhere?

Here is my code to get the path to 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 download xml, I do the following:

XmlDocument xDoc = new XmlDocument();
xDoc.Load(Server.MapPath(url));

The return path is correct, as I tested it in the anchor tag to see if it would reference the xml file, and it does. I found similar posts on this site, but none of them relate to media library items in the context of xml.Load.

Any information on whether this is possible or what I can do to make it work would be greatly appreciated.

Thank.

+5
source share
3 answers

Use a media stream instead of trying to pass a path or use 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);
+6
source

Server.MapPathwill try to match the relative or virtual path to the physical file / folder on your server.

Try removing MapPath and go to xDoc.Load(url);. Alternatively, you can load the XML document using WebClient and pass the string to your XmlDocument:

using (var client = new WebClient())
{
    string strXml = client.DownloadString(new Uri(url));
    xmlDoc.LoadXml(strXml);
}
+6
source

, XML ( JSON) Sitecore Media.

    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 hard-coded path of the object, you can use the xml file as a data source and read the xml file by reading the current rendering element.
You can go through the following source for full implementation.

Source: Reading XML / JSON from a Media Library in Sitecore MVC

+1
source

All Articles