I have this line of code that retrieves an XML file and saves it as an SPFile object

SPFile XMLFile = SPContext.Current.Web.GetFile("C:\\Users\\maleem\\Documents\\XMLTest.xml");

I want to get the XML/Text within it and output it to a literal, I tried

StreamReader  reader = new StreamReader(XMLFile.OpenBinaryStream());

...and a few variants but its not working.

2 Answers 2


I have used code like this successfully

SPFolder fldr = web.Folders[strLibraryUrl];
SPFile file = fldr.Files[XMLFileUrl];
XmlDocument xmlDoc = new XmlDocument();
byte[] bites = file.OpenBinary();
Stream strm = new MemoryStream(bites);

// Now you can select nodes, loop, or whatever
XmlNodeList nodes = xmlDoc.SelectNodes("Files/File");
foreach (XmlNode node in nodes)
    // Your logic here

Try this snippet, from here:

string site = "http://servername/sites/TestSite";     
using(SPSite spsite = new SPSite(site))

 using(SPWeb spweb = spsite.OpenWeb())              

   SPFile file = spweb.GetFile(strFileURL);

   StreamReader reader = new StreamReader(file.OpenBinaryStream());
   XDocument xdoc = XDocument.Parse(reader.ReadToEnd());
   xDoc = xdoc.ToXmlDocument();

Does it give you an error or what is the outcome?

OR, modified from comment in same link:

string strFileURL = "SiteAssets/SiteNavigation/AMSNav.xml";

string xmlString = SPContext.Current.Site.RootWeb.GetFileAsString(strFileURL);
XDocument xdoc = XDocument.Parse(xmlString);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.