I am trying to read an xml file that is present in one of the projects in my VS solution. Here's my code.
string path = HttpContext.Current.Server.MapPath(#"~\Resources\XmlDocument.xml");
XDocument document = XDocument.Load(path);
This works fine if my xml file is in the web project. But in reality I have my xml file in a different project under the same solution.
I cannot use Server.MapPath as this searches web project. I searched for an answer but did not find any solution that worked for me.
How can I access this xml file? I am trying to access this from a helper method in the same project.
Try this:
string path = HostingEnvironment.ApplicationPhysicalPath + (some path);
public void LoadXML()
{
if (System.IO.File.Exists(path))
{
XmlSerializer serializer = new XmlSerializer(typeof(List<int>));
lock (new object())
{
using (TextReader reader = new StreamReader(path))
{
return (List<int>)(serializer.Deserialize(reader));
}
}
}
}
this is working for me.
Once you get your current directory, why not just navigate up a number of parent directories and then locate your file?
DirectoryInfo info = new DirectoryInfo(Environment.CurrentDirectory);
DirectoryInfo parent = info.Parent.Parent;
string file = Path.Combine(parent.FullName, #"your\path");
Here I use Environment.CurrentDirectory to get where the program is at now, but you can stack as many Parent elements as you need. Then you can combine the path to get to your file in the other project.
EDIT:
Once your application is running all the environment variables and Server.MapPath will give you the location that it is executing from (i.e. the path you are getting now). In light of this it may be easier to create a folder and reference this via it's full path. (C:\Data\myfile.xml)
Related
firstly apology if this has already been answered and I am duplicating the question. I have tried to find the answer to my issue but have failed and none of the auto-suggestions answers my problem.
I have my main project (XAML) and also a class library project called FileStore for files. The class library project is referenced into the main project and I have images and icon file in the class library project that I can access with no issues in my main project, however, I struggle to get the content of a txt file from the CL project to display in a label on the main project. I get the error: the system could not find the file and from the error, I can see that it is trying to look for a file in the main project bin\debug folder
I tried to follow this previous post which seemed to partly answer my issue but to no avail sadly.
Get relative file path in a class library project that is being referenced by a web project
The txt file Build action is set to: Resource and Copy to Output Directory set to: Copy Always.
As I mentioned I have the FileStore project referenced in my main project and the images work fine.
Below is the code I am using, I have tried different variations such as:
\Resources\textFile.txt and \textFile.txt, still no luck.
'''
public static string ReadFileinClLibr()
{
var buildDir =
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var filePath = buildDir + #"\textFile.txt";
return File.ReadAllText(filePath);
}
'''
For comparition here is the path for the image files that works, but I cannot get it to work with the txt file, as the error reads: the given paths format is not supported..
'''
#"pack://application:,,,/FileStore;component/Resources\textFile.txt"
'''
I want to be able to input the content of the text file from the class library project to the label in the main xaml project.
At the moment compiler keeps looking for this file in a debug folder of the main project, what I want is, for the compiler to look for the txt file in a CL FileStore project
In order to access the file all the time, we have to have the file copied to the debug folder. Right click the file from solution explorer change the properties then try to access the file from the executing assembly location.
StringBuilder bodyContent = new StringBuilder();
string fileName = "myfile.txt";
try
{
string filePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), fileName);
using (StreamReader sr = new StreamReader(filePath))
{
// Read the stream.
bodyContent.Append(sr.ReadToEnd());
}
}
catch(Exception ex)
{
Console.WriteLine(String.Format("{0} # {1}", "Exception while reading the file: " + ex.InnerException.Message, DateTime.Now));
throw ex;
}
Thanks to the post from #Sreekanth Gundlapally I have managed to fix my issues. I have mostly drawn on from the answer provided by #Sreekanth Gundlapally but there is one important bit missing. The string fileName should include any subfolders that the resource file is within in the Class Library Project, for example in my case the folder was named 'Resources' so the code should look like this:
string fileName = #"Resources/myfile.txt";
try
{
string filePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), fileName);
using (StreamReader sr = new StreamReader(filePath))
{
// Read the stream.
bodyContent.Append(sr.ReadToEnd());
}
I have also cleaned and rebuilt solution after which it all worked a charm.
Also a side note, anyone trying this and getting funny characters make sure your file's encoding is set to UTF-8 as this is the default encoding used by StreamReader, otherwise your file content may not be read correctly if it contains signs such as apostrophe.
I'm making an application that loads and saves profile nodes to an external xml document in my output directory. It worked fine when I was opening it from my Assets folder but since that is read only (I think) I need to have it read and write from the output directory or another folder.
Like this:
XmlDocument users = new XmlDocument();
users.Load("users.xml");
However I get this error when this code runs:
"System.IO.FileNotFoundException: Could not find file "/users.xml"."
I've ticked the secondary storage permissions but i'm still a bit confused about just referencing a file in the output directory.
Would also appreciate the help for saving too as I assume the same error will occur:
users.DocumentElement.AppendChild(user);
users.Save("users.xml");
Thank you in advance.
In xamarin form you can try this code to load xml file from PCL project.
var assembly = typeof(TestClass).GetTypeInfo().Assembly;
Stream stream = assembly.GetManifestResourceStream(“PrjectName.FileName”);
using (var reader = new System.IO.StreamReader(stream))
{
var serializer = new XmlSerializer(typeof(List<BuildOptions>)); var listData = (List<T>)serializer.Deserialize(reader);
}
I am fairly new to developing for android using Xamarin.
I am making an application that translates dietary requirements into other languages and I am trying to load the options that one can input when making a translation.
To do this the text is put into a list after I have read the XML file:
XmlDocument LifeStyles = new XmlDocument();
LifeStyles.Load("engLifestyles.xml");
and the xml is located here:
File Location In Project
However i keep getting this error where the file is named when i run the app:
Unhandled Exception:
System.IO.FileNotFoundException: Could not find file "/engLifestyles.xml". occurred.
Any help will be much appreciated. Thank you in advanced.
In Xamarin.Android project you have to save your file in Assets folder, then you can load it. Assets folder in Xamarin is way to include arbitary files like text, xml, music etc.
Asstes folder files are readonly so if you try to write to your xml file you will get System.UnauthorizedAccessException
So your code should look like this:
XmlDocument LifeStyles = XmlDocument.Load(Assets.Open("engLifestyles.xml"));
or you can use AssetsManager too:
AssetManager assets = this.Assets;
XmlDocument LifeStyles = XmlDocument.Load(assets .Open("engLifestyles.xml"));
Here's the solution I ended up using. First, place the xml file into the Assets directory. Open it's properties and mark it's Build Action as AndroidAsset. Mark it's Copy to Output Directory as Copy if newer or some such. These will ensure your xml file is readable and is shipped with your package.
After all of that, you can then do this. In my case I wanted to load a bunch of strings into a dictionary.
private async void LoadStrings()
{
try
{
using (Stream stream = await FileSystem.OpenAppPackageFileAsync(STRING_FILE))
{
XmlDocument localInfo_Xml = new XmlDocument();
localInfo_Xml.Load(stream);
XmlNodeList tagList = localInfo_Xml.GetElementsByTagName(STRING_KEY);
foreach (XmlElement xmlElement in tagList)
{
string name = xmlElement.GetAttribute(NAME_KEY);
string value = xmlElement.InnerText;
strings.Add(name, value);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
throw ex;
}
}
Hope that helps someone else's day get better.
I'm trying to load an XML file from a separate project; One of these projects is a game engine, which calls the XML document reader and takes in a path specifying the relative directory to the file.
From Engine
XDocument doc;
try
{
Stream stream = this.GetType().Assembly.GetManifestResourceStream(path);
doc = XDocument.Load(stream);
}
catch
{
doc = XDocument.Load(path);
}
From the other project
string filePath = "Test.xml";
Npc npc = new Npc("somename", 2,filePath);
Test.xml resides in the other project's root directory. The Npc constructor makes a call to a Statistics object constructor, which then calls the method which loads the XDocument. As this is happening, the filePath is simply passed downward through the layers.
I've looked at this, and tried the embedded resource example, which is ultimately what I'm trying to accomplish, and it didn't work for me.
What am I doing wrong, here?
Update
I changed Text.xml to Chronos.Text.xml, as that is where the file resides. In my debugger, I see that the stream simply returns null when I use that as a path:
try
{
Stream stream = this.GetType().Assembly.GetManifestResourceStream("Chronos.Test.xml"); //returns null
doc = XDocument.Load(stream); //Exception thrown
}
catch
{
doc = XDocument.Load(path); //File not found
}
Embeded resources are embeded directly in the executable. Assembly.GetManifestResourceStream() is trying to open a stream on the embeded resource, what you should be providing is the resource name in the following format AssemblyDefaultNamespace.Directory.Filename.
If you are trying to open an XML file from another directory, you will have to provide the full path to XDocument.Load(), or a path relative from your current project output directory pointing to that other directory.
Another solution would be to copy the data from the other project into your project and specify that you want the file to be copied to your output directory.
I have a static class in a folder off root in my solution. In that static class' folder, there's a subfolder containing XML files. So I've got these files:
/PartialViews/Header/MyStaticClass.cs
/PartialViews/Header/Config/en-US.xml
/PartialViews/Header/Config/jp-JP.xml
...
I'm having trouble using XDocument.Load() with those XML files. Specifically, I'm trying to load the XML files from the static constructor of MyStaticClass.
XDocument.Load() can't seem to find the files, however. I've tried all these and none work:
static MyStaticClass()
{
XDocument doc;
// These all throw exceptions relating to directory not found
doc = XDocument.Load("/Config/en-US.xml");
doc = XDocument.Load(#"\Config\en-US.xml");
doc = XDocument.Load("/PartialViews/Header/Config/en-US.xml");
doc = XDocument.Load(#"\PartialViews\Header\Config\en-US.xml");
}
I also tried using Assembly.GetExecutingAssembly().Location and Assembly.GetEntryAssembly().Location before the relative path, but the assembly resolved by Assembly is always a .NET library (because the type is being initialized?).
How can I load the file without changing its location in the solution?
In ASP.NET you should use Server.MapPath() to find all local files.
string relPath = "~/PartialViews/Header/Config/en-US.xml";
string absPath = Server.MapPath(relPath);
XDocument doc = XDocument.Load(absPath);
For .NET web apps use
HttpContext.Current.Server.MapPath("~/"); this will get you the root directory of the executing file.