I am developing a webpart that should read an XML file. For this I have created a class, in which the NodeValue() method should return the text inside of nodes. This does work if tested with a console application, however when I add this(reader class and custom XML file) to my SharePoint project and try to deploy it, I get the error message Web Part error: File Not Found. I tried changing the file properties so as it is added to the compiled version, but I still seem to have trouble with specifying the path to the file. Here is the reader class:
using System;
using System.Reflection;
using System.IO;
using System.Xml;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OrgChartPart.WebPartCode
{
class ReadXml
{
public string FilePath { get; set; }
public ReadXml()
{
FilePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)+"\\LAYOUTS\\config.xml";
}
public string NodeValue(int pos)
{
if (pos <= 0)
return "invalid position!(this is not a node value)";
XmlTextReader textReader = new XmlTextReader(FilePath);
int counter = 0;
while (counter != pos)
{
textReader.Read();
if (textReader.NodeType.ToString() == "Text")
counter++;
}
return textReader.Value;
}
}
}
And here is a part of the web part code where I instanciate this class to read the file:
StringBuilder sb = new System.Text.StringBuilder();
.......
string position = null, link_type = null, link_color = null,
node_alignment = null, node_fill = null, node_color_style = null, hyperlinks = null;
ReadXml readXml = new ReadXml();
sb.Append(@"
........
I don’t know if I am having trouble with identifying the path of the file, or do I need to have heighten permission to access it? Or maybe when the solution gets deployed the directory structure is changed, so I am specifying a wrong path? What do you think?
Thanks in advance
Well, I managed to solve it..somewhat..
I added the XML file(or CSS or whatever u want) via SharePoint API(on the farm), after which I was able to access the file using the virtual path of the file(u can see the directory structure using Sharepoint Design Manager and opening your farm),