How to read XML document from property in Episerver

517 Views Asked by At

I want to read XML document from a property which is created in edit mode of Episerver.

I have made one property of type 'URL to Document'. When I try to fetch it from code behind, it gives only file path. I am not able to read the content of XML file which is uploaded in property.

string XMLContent = Currentpage.Getproperty<string>("XMLFile");

Can anyone help out on this?

2

There are 2 best solutions below

7
Johan Petersson On

You need to load the file as well. Something like this:

var path = CurrentPage["XMLFile"] as string;

if (HostingEnvironment.VirtualPathProvider.FileExists(path))
{
    var file = HostingEnvironment.VirtualPathProvider.GetFile(path) as UnifiedFile;

    if (file != null)
    {
        using (var stream = file.Open())
        {
            // Here is your XML document
            var xml = XDocument.Load(stream);
        }
    }
}

You can also load the file content by using the local path on disk, file.LocalPath.

1
jdweng On

try this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string XMLContent = "";

            //using XML
            XmlDocument doc1 = new XmlDocument();
            doc1.LoadXml(XMLContent);

            //using xml linq
            XDocument doc2 = XDocument.Parse(XMLContent);
        }
    }
}
​