Error replacing xml file #C:\Windows\System32\WindowsPowerShell - c#

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

Related

GenericPackager can't read XML file in ISO Message

I am trying to call in ISO Message from Website (ASP.NET). I imported all DLL and code. But while reading XML from GenericPackager class, file can't read exception is coming.
The same code is working fine in C# Windows application.
Here is the code:
IsoXML_Path = HttpContext.Current.Server.MapPath("~") + "PostCardFields.xml";
GenericPackager packager = new GenericPackager();
packager.readFile(IsoXML_Path);
ISOMsg isoMsg = new ISOMsg();
isoMsg.setPackager(packager);
Error in Exception:
Error reading (PATH)\PostCardFields.xml
(org.xml.sax.SAXException: SAX2 driver class org.apache.crimson.parser.XMLReaderImpl not found
java.lang.ClassNotFoundException: org.apache.crimson.parser.XMLReaderImpl)
(PATH) is the path of my XML file in directory. File is exists on that path.

Get path from XML File using C#

I'm needing to get a path say C:\SourceFiles\ from an XML File using C#.
I have been trying different escapes methods, but nothing seems to work correctly.
I have tried these:
#"C:\SourceFiles\ or C:\\SourceFiles\\ or "C:\\SourceFiles\\" and 'C:\SourceFiles'
None of these seem to work when reading from an XML file.
XmlDocument xDoc = new XmlDocument();
xDoc.Load(strpath);
string strsourceDirectory = xDoc.SelectSingleNode("Application/Setup/SourceDirectory").InnerText;
Here is the XML File:
<Application>
<Setup>
<SourceDirectory>"C:\SourceFiles\"</SourceDirectory>
<DestinationDirectory>#"C:\DestinationFiles\"</DestinationDirectory>
</Setup>
If someone has done this with C# and a XML file, please let me know how you did it.
Thanks,
Your XML file is invalid. You need to escape the backslash \\ and close the Application tag:
<Application>
<Setup>
<SourceDirectory>C:\\SourceFiles\\</SourceDirectory>
<DestinationDirectory>C:\\DestinationFiles\\</DestinationDirectory>
</Setup>
</Application>
With this valid XML, you will be able to get the path using your code:
string strsourceDirectory =
xDoc.SelectSingleNode("Application/Setup/SourceDirectory").InnerText;

load xml file after reading in string

I have read an xml file as a string due to cryptography.
string xmlString = System.IO.File.ReadAllText("../../liberal.xml");
//how to load xml document here?
XmlDocument xmlDo = new XmlDocument();
xmlDo.Load("../../liberal.xml");
The above code throws error and doesn't load.
XML file doesn't have any root elements and right now liberal XML file looks like this dasjkhf8dfkbhdflak3kjbdf+fafas(safasasdfjgdskalfguv.ng;FHSDAFKLASDF.
Couldn't load xml document with this data format. Only if I can load XML document I will be able to use their properties to add new values to the xml file.
Update1:
I decrypted the xml and placed in a string, but couldn't load the xml document with that string.
string retValue;
XmlDocument dec = new XmlDocument();
dec.Load(retValue);
retValue string has values like this.
<Product><Type>Metal</Type><Department>Foundry</Department></Product>
Error Message
Illegal characters in path.
Really appreciate any help with this.
You're using the XmlDocument.Load(string) method which accepts a path to an XML file. You need to use the XmlDocument.LoadXml(string) method which accepts any valid XML markup. Two completely different parameters. Example:
// XmlDocument.LoadXml(string)
string decryptedMarkup = "<Product><Type>Metal</Type>"
+ "<Department>Foundry</Department></Product>";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(decryptedMarkup);
// XmlDocument.Load(string)
string pathToFile = "test.xml";
XmlDocument xmlDoc2 = new XmlDocument();
xmlDoc2.Load(pathToFile);
For further information, take a look at:
XmlDocument.Load(string)
String parameter:URL for the file containing the XML document to load. The URL can be either a local file or an HTTP URL (a Web address).
XmlDocument.LoadXml(string)String parameter:String containing the XML document to load.

loading xml document error in c#

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.

Loading resources from another project?

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.

Categories