0

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?

freshbm
  • 5,540
  • 5
  • 46
  • 75

2 Answers2

3

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.

Johan Petersson
  • 972
  • 4
  • 13
  • Hi, Thank you for the answer. But var stream = file.Open() is not working. The file content is not getting fetched in stream object. Error: '((System.IO.Stream)(s)).ReadTimeout' – user2130929 Oct 11 '15 at 14:13
  • Can you open the file, which you have specified in the property, in the file gadget at all? The error suggests that you have issues reading the file from disk. What does file.LocalPath say? Does it give you a path that exists on disk? – Johan Petersson Oct 12 '15 at 13:25
  • Yes, file.LocalPath gives me correct path. The stream variable gives me the file information but how can I read the content of it. – user2130929 Oct 12 '15 at 14:16
  • The xml may have errors. You need to validate if the xml is correct. From VS Project Menu : Add New item : XML file. Paste the xml into window. Errors will show up in the Error List window just like any compiler errors. – jdweng Oct 12 '15 at 14:25
  • XDocument.Load(stream) is not taking stream as a parameter. XDocument.Load takes string or xmlreader as a parameter. – user2130929 Oct 13 '15 at 11:17
  • with a stream you don't get all the data at one time. So you can't Load(XML) until the entire data is in the stream. It looks like the file is coming over a network which an take time especially with a large XML. How large is the XML? – jdweng Oct 13 '15 at 18:52
  • XDocument.Load does take a stream https://msdn.microsoft.com/en-us/library/cc838321(v=vs.110).aspx – Johan Petersson Oct 14 '15 at 11:27
0

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);
        }
    }
}
​
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • The problem is not related to parsing the content as XML. The question is actually related to the product EPiServer and how to read a file. – Johan Petersson Oct 12 '15 at 13:28