Copy one Xml Document contents into another one in C# - c#

I am trying to copy contents of one xml file into another xml file. I found many examples where copying nodes is done but could not find how to just copy all the content.
Is this possible at all? If so can you please provide some direction.
Thanks
Edit:
I want to create this new xml file in the location dynamically supplied by the application's text box.
Thanks again.

If you want to replace one XML file with another, why not use File.Copy?

As a file:
string sourcefile = "somefile.xml";
string destinationfile = "anotherFile.xml";
System.IO.File.Copy(sourcefile, destinationfile);

Is File.Copy() what you're looking for?

Related

Open a file without dialog

with openFileDialog you would select a file and after pressing "OPEN" it would paste the filepath of the selected file (c:\blob\template) into an textbox.
I would like to do automatically select the file c:\blob\template en then put the filepath in the textbox. basically the exact same thing as openfiledialog without a dialog. i have been trying to do this for some time now. can somebody help me out with this? i have no clue how to realise this.
i`m only able to get the filepath and paste the string in the textbox but this but only fills the box with a string. i need to load the file/template in there.
private void txt()
{
string fileName = "template";
string fullPath;
fullPath = Path.GetFullPath(fileName);
lblFirstTemplate.Text = fullPath;
}
Thank you in advance!
The code you now have will only get the file path. What you need to add is code that will actually open the file and read its content.
Let's say your file contains some text. You can use the following line to read the complete file as text:
System.IO.File.ReadAllText(fullPath);
If your file contains some other data like binary data, you can use:
System.IO.File.ReadAllBytes(fullPath);
And instead of reading all data at once, you can read it one line or a couple of bytes at a time. A good place in the documentation to start is: Common I/O Tasks

Reading a file added manually in project as resource in VisualStudio

In my soloution, for one of the projects I have to add a binary file and read its content in the form_load event.
As you can see in the picture I have added it to the appropriate project and have set the Build Action to Content and Copy to Output Directory as Copy Always.
Now can somone please tell me how how to access this file?
private void SetupForm_Load(object sender, EventArgs e)
{
//Find the path to file then
//READ THE FILE
}
Now you should find this file in your output directory after building your project. Given the right path to the file, you can access this file with any method you want.
Some methods can help you to get the path to the file:
Directory.GetCurrentDirectory();
Environment.CurrentDirectory
I'm not sure exactly what you're trying to do with the file, and that will determine your best approach here. As per the answer here you have a couple of options. To paraphrase:
Serialization
Binary Reader
I think the best method was this, so far:
So when I add the file to the project, it will be in the same folder as the exetubale file of project resides. So for getting the path (including the name of exetuable file I had to use Application.ExecutablePath and to remove the file name and have the pure path to the folder I had to use Path.GetDirectoryName() and finally add the filename I wanted to acces to this path, as you can see below:
var path = Path.GetDirectoryName(Application.ExecutablePath) + "\\YourFileName.bin";
The API you call to read the file depends upon the type of file. But the general pattern is like this.
private void SetupForm_Load(object sender, EventArgs e)
{
System.IO.Stream input = Application.GetResourceStream(new Uri #"/MyApp;component/content.bin", UriKind.Relative)).Stream;
BinaryReader binaryReader = new BinaryReader(input);
}
You can directly readbytes from the file attached as resource.My resource in this example is SampleWordnew document.
byte[] bob = ReadBytesfromResources.Properties.Resources.SampleWordnew ;

Open CHM (help file) in C#

I'm trying to open help file (chm extension) in C#.
File.Open(#"//help.chm",FileMode.Open, FileAccess.Read, FileShare.Read);
and
FileStream fileStream = new FileStream(#"c:\help.chm", FileMode.Open);
doesn't work :(
You can use -
System.Windows.Forms.Help.ShowHelp(Control, String)
So assuming you are in a Form/Control
Help.ShowHelp(this, "file://c:\\helpfiles\\help.chm");
ShowHelp method also provides overloads to go to specific topic and help page located inside the compiled HTML help file.
Read System.Windows.Forms.Help.ShowHelp on MSDN
Decompiling a CHM file
Is as easy as executing below command in the command prompt.
hh.exe -decompile <target-folder-for-decompiled-content> <source-chm-file>
For Example:
hh.exe -decompile C:\foo\helpchmextracted help.chm
After executing the above command you should find the decompiled content in the C:\foo\helpchmextracted folder.
string helpFileName = #"c:\help.chm";
if (System.IO.File.Exists(helpFileName))
{
Help.ShowHelp(this, helpFileName );
}
if this is not work try
if (System.IO.File.Exists(helpFileName))
{
System.Diagnostics.Process.Start(helpFileName);
}
Adding my comments to an answer as per request:
It seems that the file name in the first statement is not correct however the second one
should work unless the file is locked, not exists or you don't have permissions to access the file. If you want to ShellExecute the file then you should use System.Diagnostics.Process class, but if you want to extract the contents of the CHM, since is compiled and formatted, it can't be read like plain text files.
Take a look at these links:
Decompiling CHM (help) files with C#
CHM Help File Extractor
Well the second line should be ok, if the file is not existent it should throw an exception. Need to be more specific about what you mean by " it doesn't work"
Help.ShowHelp(this, AppDomain.CurrentDomain.BaseDirectory+"\\test.chm", HelpNavigator.Topic, "Welcome.htm");
Welcome is id of the welcome age in chm file
System.Diagnostics.Process.Start(#"c:\help.chm");
Simple do this
Help.ShowHelp(ParentForm, "chmFile.chm", "link.htm");

Deleting particular text in a file using c#?

My text file named test.txt contains
Fixing the type manager error during checkin of the elements and supporting issues during Checkin/Checkout & merge related problems.
Now i want to remove the text "supporting issues" using c#.
Please anybody let me know.
Thanks in advance,
Naveenkumar.T
if the file is fairly small you can do this:
using System.IO; // at the top of the file
string file = "path.txt"; // change to the path
// read the whole file into a string, make a replacement, then write all string to a file
File.WriteAllText(file, File.ReadAllText(file).Replace("supporting issues ",""));
Hope that helps.

How to save one file from one path to another path? Without removing file from original path using C#

I want to save one file from one path to another path.
Eg:
Original path : C:\File.txt
New path : D:\Folder\File.txt
Can i use:
File.Move("C:\File.txt", "D:\Folder\File.txt");
But i think this will remove file from original path. (C#)
Try File.Copy, i.e.
File.Copy(#"C:\File.txt", #"D:\Folder\File.txt");
You can use
File.Copy Method
Copies an existing file to a new file.
How to: Copy, Delete, and Move Files and Folders (C# Programming Guide)
Have you tried File.Copy(#"C:\File.txt", #"D:\Folder\File.txt");?
Have you tried File.Copy ?
Then you could use File.Copy.
Try File.Copy instead.
Just like this below...
System.IO.File.Copy(sourceFileName, destFileName);

Categories