I'm trying to load a document with xml in c#
the name of xml file is variable, here is problem...
string filename="test01.xml";
XmlDocument root = new XmlDocument();
root.Load(filename);
the above code give me error: unable to connect to remote server or unable to load
but the following code works
XmlDocument root = new XmlDocument();
root.Load("test01.xml");
why is that?
You can try to specify the whole path (absolute path) to the file (not only the filename).
So instead of writing "test01.xml" you can try to write "C:\[... path to the file here]\test01.xml" and it should work as intended.
If you specify only the file name, the application will probably look for the file in the current directory (value in Environment.CurrentDirectory). I just tested this in a sample application.
It's worth mentioning that if you use FileName property from OpenFileDialog class as a case with 'using variable', it contains PATH to the file (despite its name ;)).
Does your XML contains DTD declaration with URL? Most probably parser tries to resolve it, and fails, because, say, automatic proxy does not accept its request.
Related
I have a problem. Not very big but didn't find any answer.
I want to sore some key value pair in file Data.xml and I have saved it at root level.
When I am trying to use code below.
XDocument xmlDoc = new XDocument();
xmlDoc.Load("Data.xml");
Instead of checking at project root level it is checking for some other location. I tried current directory and environment.current path. But it didn't help.
I don't want to specify full path. Because when application will go live I don't want to change it.
I need to use data.xml so that if values got changed we will just replace the data.xml so get new values.
It's looking in the current directory that the application is running (bin\release or bin\debug). What you want to do is make sure that you have that XML file in your project set to Copy to Output Directory
You may want to prefix the name parameter with Application.StartupPath() for rigidity.
I got my answer from another post.
Click here
Below code can be used.
string dirpath = System.Web.HttpContext.Current.Server.MapPath("~/Data.xml");
XDocument xmlDoc = new XDocument();
xmlDoc.Load(dirpath);
I am writing a console application where I'm trying to replace a xml file (xx.config) with other xml file(xx.config) which is at different folder with different data to the path C:\Windows\System32\WindowsPowerShell.
I'm getting following error
Could not find a part of the path C:\Windows\System32\WindowsPowerShell\xx.config
But there is the file at this path. I tried to load this file to XMLDocument then also same error occurred. Can anyone tell me what I'm doing wrong.
XDocument xmldoc = XDocument.Load(#"C:\test\xx.config");\loads good
XDocument xmldoc = XDocument.Load(#"C:\Windows\System32\WindowsPowerShell\xx.config");\error occurs
File.Move(#"C:\test\xx.config", >#"C:\Windows\System32\WindowsPowerShell\xx.config");\error occurs
I am learning MVVM & Linq to xml by converting a simple wpf RSS app. Originally, I used XmlDataProvider to load local xml files or RSS urls. I am now using the code below which works fine loading local xml files, but throws the "FileNotFoundException" when its a url. During debugging I see the correct url address in (string RSS_URL), yet the file is not found. My initial searching led me to Webclient & HttpWebRequest, but I haven't had any success with them. Am I on the right track? Any code or tutorial links available?
public static List<RSSItem> Load(string RSS_URL)
{
if (!File.Exists(RSS_URL))
{
throw new FileNotFoundException("Datasource file could not be found", RSS_URL);
}
var rssfiles = XDocument.Load(RSS_URL).Descendants("item").Select(
x => new RSSItem(
(string)x.Element("title"),
(string)x.Element("link"),
(string)x.Element("description")));
return rssfiles.ToList();
}
Thank You
XDocument.Load() will accept URLs without any problem. The issue in your code is that you're using File.Exists() to determine whether or not he URL is valid. File.Exists() only accepts a filesystem path, not a uri.
Quick piece of additional info: the Load() method relies on an underlying XmlReader and a call to Create(). If the resource (the URL in this case) doesn't exist, a WebException will be thrown indicating that the resource doesn't exist.
XDocument.Load info:
http://msdn.microsoft.com/en-us/library/bb343181.aspx
XmlReader.Create info:
http://msdn.microsoft.com/en-us/library/w8k674bf.aspx
The XDocument.Load overload you are using is specifically for loading from a file. You would have to download from the RSS feed to get the XML data locally. Look at the MSDN document here for alternatives such as reading from a stream, which might be better suited to what you need.
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.
Is there a way of returning an xml file from a REST webservice. I want to be able to call the server, dynamically create the file and return as if I'd simply returned the xml file as stored on disk.
I've managed to get fairly close by returning an XMLElement:
public XmlElement Airports()
{
//Dynamically build up and return
}
But when I referenced the location from a DTD it doesn't seem to behave in the same way e.g.
This works:
ENTITY XmlFile SYSTEM "http://localhost:59736/MyXmlFile.xml"
But this doesn't
ENTITY XmlFile SYSTEM "http://localhost:59736/MyService.svc/MyMethod"
There must be some subtle difference in the headers or something...
I assume your are not returning a file name or anything like that but the file contents as an XML response. In that case it really is no different if you return the contents of an existing file or generate the contents on the fly.