Populating xml using c# error - c#

I am populating an xml document using c#. If I populate it once then everything is ok but when I try to repopulate it a second time (without closing the program) I get an error message and rubish gets written to the bottom of the xml file.
I'm sure I've found the answer to this question before but I can't find it.
I'm sure its something to do with the fact that I'm not closing something down after updating the xml document or something but I can't remember what exactly I have to close down.
Sorry I hope you all understand. I find it difficult to explain.
Code:
using (FileStream READER = new FileStream(fpath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
System.Xml.XmlDocument Template = new System.Xml.XmlDocument();// Set up the XmlDocument //
Template.Load(READER); //Load the data from the file into the XmlDocument //
//**********Grab nodes to be written to********
using (FileStream WRITER = new FileStream(fpath, FileMode.Open, FileAccess.Write, FileShare.ReadWrite))
{
//Set up the filestream (READER) //
//Write the data to the filestream
Template.Save(WRITER);

You read and write in the same file fpath
You have to close the reader after Template.Load(READER);
using (FileStream READER = new FileStream(fpath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
System.Xml.XmlDocument Template = new System.Xml.XmlDocument();// Set up the XmlDocument //
Template.Load(READER); //Load the data from the file into the XmlDocument //
}
//**********Grab nodes to be written to********
using (FileStream WRITER = new FileStream(fpath, FileMode.Open, FileAccess.Write, FileShare.ReadWrite))
{
//Set up the filestream (READER) //
//Write the data to the filestream
Template.Save(WRITER);
...
}

Related

NPOI 2.6.0-rc-3 with .net framework 4.8.1 for updating .xlsx - losses charts in excel file

I use NPOI 2.6.0-rc-3 with .net framework 4.8.1 for updating .xlsx file. Excel file includes charts with it. excel file get crashed and after saving. After recovering the same file it losses charts in excel.
Used the following code.
XSSFWorkbook wb1 = null;
using (var file = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite))
{
wb1 = new XSSFWorkbook(file);
file.Close();
//Updated the cell values here
using (var file2 = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite))
{
wb1.Write(file2);
file2.Close();
}
}
Please help regrading this matter
Try to modify the cells out of the using block (you dont need to keep the file open to modify the IWorkbook) and then save it using a diferent stream:
IWorkbook wb1 = null;
using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
wb1 = new XSSFWorkbook(file);
}
//Updated the cell values here
using (FileStream fileWrite = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
wb1.Write(fileWrite);
}

Read file that's already used by another process

I have a C# app that tries to read a log file which is being written to by another app. When I try to read the file, I get IOException
"The process cannot access the file ... because it is being used by
another process."
What I tried using so far are the following, but none of them fix the problem
var log = File.ReadAllText(logPath);
var stream = new FileStream(logPath, FileMode.Open);
using (var stream = File.Open(logPath, FileMode.Open))
{
}
try this:
FileStream logFileStream = new FileStream("c:\test.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader logFileReader = new StreamReader(logFileStream);
while (!logFileReader.EndOfStream)
{
string line = logFileReader.ReadLine();
// Your code here
}
// Clean up
logFileReader.Close();
logFileStream.Close();
edited with MethodMan's suggestions
using(FileStream logFileStream = new FileStream(#"c:\test.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using(StreamReader logFileReader = new StreamReader(logFileStream))
{
string text = logFileReader.ReadToEnd();
// Your code..
}
}
You can do nothing, if the "another app" does not use Share.Read while creating/opening the file.

Many FileStream's attached to one file

Like in title:
FileStream fs = new FileStream ("test.mkv", FileMode.Open);
FileStream fs1 = new FileStream ("test.mkv", FileMode.Open);
It throw an error: The process cannot access the file '...' because it is being used by another process.
But why, if it is opened for read only (?). If not, how to open file as read only?
You need to specify that you're opening it read only and that you're sharing it.
var fs1 = new FileStream("test.mkv", FileMode.Open, FileAccess.Read, FileShare.Read);
var fs2 = new FileStream("test.mkv", FileMode.Open, FileAccess.Read, FileShare.Read);

Xml editing problem using C#

I have posted a question previous regarding editing my xml document via c#
C# write to XML error
However im now having trouble with it again. Im using the exact code that worked then but getting problems again!
When I first click the button it seems to work however when I click it again I get the error
Data at the root level is invalid. Line 83, position 10
When you then open the XML document for some reason the characters "" get added to the start of the xml document so I get
"<?xml version="1.0" encoding="UTF-8"?>"
I dont understand why and its really driving me insane. I'm sure it was working before.
My code:
path = test.xml
using (FileStream READER = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
System.Xml.XmlDocument Temp = new System.Xml.XmlDocument();
Temp.Load(READER);
using (FileStream WRITER = new FileStream(path, FileMode.Open, FileAccess.Write, FileShare.ReadWrite))
{
Temp.Save(WRITER);
}
}
UPDATE #2:
I compiled your sample as is and it worked perfectly for me. I tested it with a file created straight from code and also with a xaml file created in Visual Studio. So it seems the file you're working with is corrupted or have an encoding problem.
As far as I know you can't do anything about corrupted file, but as for encoding you can specify it when reading by using a StreamReader object. You just pass a desired encoding and your reader stream to StreamReader's constructor. Also it has an option to detect the encoding from byte order marks.
using (TextReader txtreader = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.GetEncoding(1251 /*desired codepage here*/)))
{
document.Load(txtreader);
}
or
using (TextReader txtreader = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), true /*tries to detect the encoding*/))
{
document.Load(txtreader);
}
Of course, you should save the file using the same encoding or you'll have problems next time you run your loading code.
Also I'm attaching a code which creates a file if it doesn't exist or just modifies it if it already exists.
class Program
{
static readonly string path = #"C:\Users\Dmitry\Documents\test_3.xml";
static void Main(string[] args)
{
for (int i = 0; i < 10; i++)
test(path);
}
static void test(string path)
{
XmlDocument document = new XmlDocument();
if (File.Exists(path))
{
using (Stream readStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
document.Load(readStream);
}
}
else
{
document.AppendChild(document.CreateXmlDeclaration("1.0", "UTF-8", String.Empty));
document.AppendChild(document.CreateElement("Test"));
}
document.DocumentElement.AppendChild(document.CreateElement("Node"));
using (FileStream WRITER = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
{
document.Save(WRITER);
}
}
}
I hope it helps you.

Files to binary stream

I want to fetch the files from the folder and convert to binary stream. How is it possible using ASP.Net with C# ?
A FileStream will do the job:
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
// work with fs
}

Categories