I am using following two code to load an XML.Some other process is updating the same XML while i am trying to Load it.It's giving me below error in both case:-
The process cannot access the file
'D:\Projects\WriteOnXml\Xml\test.xml' because it is being used by
another process.
1)
using (FileStream xml = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
XDocument xdoc = XDocument.Load(xml);
}
2)
using (StreamReader sr = new StreamReader(dest))
{
xdoc = XDocument.Load(sr);
}
I want to load it while it is updating by some other process.
Any better approach.
have a look on that question:
c# xml.Load() locking file on disk causing errors
it seems that if you could use xml document it will work...
Related
I'm trying to edit xml file.
but document.Save() method has to use another file name.
Is there any way to use same file? or other method. Thank you!
string path = "test.xml";
using (FileStream xmlFile = File.OpenRead(path))
{
XDocument document = XDocument.Load(xmlFile);
var setupEl = document.Root;
var groupEl = setupEl.Elements().ElementAt(0);
var valueEl = groupEl.Elements().ElementAt(1);
valueEl.Value = "Test2";
document.Save("test-result.xml");
// document.Save("test.xml"); I want to use this line.
}
I receive the error:
The process cannot access the file '[...]\test.xml' because it is being used by another process.
The problem is that you are trying to write to the file while you still have it open. However, you have no need to have it open once you've loaded the XML file. Simply scoping your code more granularly will solve the issue:
string path = "test.xml";
XDocument document;
using (FileStream xmlFile = File.OpenRead(path))
{
document = XDocument.Load(xmlFile);
}
// the rest of your code
I am writing code for creating excel spreadsheet using OpenXml. But wanted to use Filestream instead of MemoryStream in SpreadSheet.Create() method.
Note: With MemoryStream working correctly but for some reasons I need to get code working with Filestream.
Generating corrupted file when writing code like:
FileStream fs = new FileStream(filepath, FileMode.OpenOrCreate);
using (SpreadsheetDocument document =
SpreadsheetDocument.Create(fs,SpreadsheetDocumentType.Workbook))
{
//worksheet code
}
If needed I can post worksheet code.
Try to use using to ensure that the file stream will be closed.
using (FileStream fs = new FileStream(filepath, FileMode.OpenOrCreate))
{
using (SpreadsheetDocument document =
SpreadsheetDocument.Create(fs,SpreadsheetDocumentType.Workbook))
{
//worksheet code
}
}
I have been fiddling with this problem for the past hour so I thought you guys may be able to help on a Friday afternoon!
Problem
I am trying to edit an XML file in localstorage but can't figure out how to edit the existing file and re-save the file. The edit I have made it to remove a certain node from the XML.
Code
Here is the method that does all the work.
This first code snippet was already in the code and basically creates the XML file and saves it to localstorage.:
protected byte[] CreateFileData(PortableBusinessObjects.Location location, string geoObjectFilename)
{
byte[] fileData = null;
var xmlFile = System.IO.Path.GetFileNameWithoutExtension(geoObjectFilename) + ".xml";
var zipFile = System.IO.Path.GetFileNameWithoutExtension(geoObjectFilename) + ".zip";
using (IsolatedStorageFileStream fileStream = localStorage.CreateFile(xmlFile))
{
XmlWriter writer = XmlWriter.Create(fileStream);
if (location.GetType() == typeof(PortableBusinessObjects.Shape))
_xmlShapeSerializer.Serialize(writer, location);
else if (location.GetType() == typeof(PortableBusinessObjects.Point))
_xmlPointSerializer.Serialize(writer, location);
fileStream.Flush();
fileStream.Close();
}
}
This is my attempt at overwriting the saved file (Doesn't work):
using (IsolatedStorageFileStream doc = localStorage.OpenFile(xmlFile, FileMode.Open))
{
System.Xml.Linq.XDocument test = System.Xml.Linq.XDocument.Load(doc);
test.Descendants("Time").Remove();
XmlWriter writer = XmlWriter.Create(doc);
doc.Flush();
doc.Close();
}
Question
Where do I place my code that removes the "Time" nodes and saves the file?
Your saving code doesn't do any saving - you just create an XmlWriter and do nothing with it.
There are various methods built into XDocument than can help you here. While you could pass your XmlWriter to it, you can actually save directly to the stream:
test.Save(doc);
Note you will need to move to the beginning of the stream before writing to it - loading your XML will have read to the end:
doc.Position = 0;
You should use the IsolatedStorageFileStream together with the StreamWriter.
See How to: Read and Write to Files in Isolated Storage
With XDocument you then have to Save() the new contents to the stream.
I'm using XDocument to update the XML file with Isolated Storage. However, after save the updated XML file, some extra characters are added automatically.
Here is my XML file before update:
<inventories>
<inventory>
<id>I001</id>
<brand>Apple</brand>
<product>iPhone 5S</product>
<price>750</price>
<description>The newest iPhone</description>
<barcode>1234567</barcode>
<quantity>75</quantity>
<inventory>
</inventories>
Then after the file is updated and saved, it becomes:
<inventories>
<inventory>
<id>I001</id>
<brand>Apple</brand>
<product>iPhone 5S</product>
<price>750</price>
<description>The best iPhone</description>
<barcode>1234567</barcode>
<quantity>7</quantity>
<inventory>
</inventories>ies>
I've spent a lot of time trying to find and fix the problem but no solutions were found. The solution in the post xdocument save adding extra characters cannot help me fix my problem.
Here's my C# code:
private void UpdateInventory(string id)
{
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = isf.OpenFile("inventories.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
XDocument doc = XDocument.Load(stream);
var item = from c in doc.Descendants("inventory")
where c.Element("id").Value == id
select c;
foreach (XElement e in item)
{
e.Element("price").SetValue(txtPrice.Text);
e.Element("description").SetValue(txtDescription.Text);
e.Element("quantity").SetValue(txtQuantity.Text);
}
stream.Position = 0;
doc.Save(stream);
stream.Close();
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
}
}
}
The most reliable way is to re-create it:
XDocument doc; // declare outside of the using scope
using (IsolatedStorageFileStream stream = isf.OpenFile("inventories.xml",
FileMode.Open, FileAccess.Read))
{
doc = XDocument.Load(stream);
}
// change the document here
using (IsolatedStorageFileStream stream = isf.OpenFile("inventories.xml",
FileMode.Create, // the most critical mode-flag
FileAccess.Write))
{
doc.Save(stream);
}
When I had a similar problem in Python, I discovered that I was overwriting the beginning of the file without truncating it afterwards.
Looking at your code, I'd say you might be doing the same:
stream.Position = 0;
doc.Save(stream);
stream.Close();
Try setting the stream length to its post-save location as per this answer:
stream.Position = 0;
doc.Save(stream);
stream.SetLength(stream.Position);
stream.Close();
I have a problem when I deserialize the xml into List of Objects. I searched it on the net this morning, but my problem isn't resolved.
Deserialization method
public static List<FileAction> DeSerialization()
{
XmlRootAttribute xRoot=new XmlRootAttribute();
xRoot.ElementName="ArrayOfSerializeClass";
xRoot.IsNullable=true;
XmlSerializer serializer = new XmlSerializer(typeof(List<FileAction>),xRoot);//, new XmlRootAttribute("ArrayOfSerializeClass")
using (Stream streamReader = File.OpenRead(#"C:\serialization\SerializationWithFileWatcher\Output\XmlSerialize.xml"))//FileStream fs =new FileStream(xmlPath,FileMode.Open)
{
using (XmlReader reader = XmlReader.Create(streamReader))
{
int count =0;
List<FileAction> serialList2 = (List<FileAction>)serializer.Deserialize(reader);
return (List<FileAction>)serializer.Deserialize(reader);
}
}
Calling Method
String resultPath = #"C:\serialization\SerializationWithFileWatcher\Output\XmlSerialize.xml";
if (!File.Exists(resultPath))
{
XmlSerializer xs = new XmlSerializer(typeof(List<SerializeClass>));
using (FileStream fileStream = new FileStream(#"C:\serialization\SerializationWithFileWatcher\Output\XmlSerialize.xml", FileMode.Create))
{
xs.Serialize(fileStream, serializeList);//seri
fileStream.Close();
}
Console.WriteLine("Succesfully serialized to XML");
}
else
{
//string path= #"C:\serialization\SerializationWithFileWatcher\Output\XmlSerialize.xml";
DeSerialization();
XmlSerializer xs = new XmlSerializer(typeof(List<SerializeClass>));
FileStream fs = new FileStream(#"C:\serialization\SerializationWithFileWatcher\Output\XmlSerialize.xml", FileMode.Append, FileAccess.Write);
using (XmlWriter xwr = XmlWriter.Create(fs))//TextWriter xwr = new StreamWriter
{
xs.Serialize(xwr, serializeList);//seri
//fs.Close();
}
Console.WriteLine("Succesfully serialized to XML");
}
return serializeList;
The reason why I am calling it here is that I want to add this object again to the xml file.
THe error is that here is an error in XML document (15,27).
My Xml structure
<?xml version="1.0"?>
<ArrayOfSerializeClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SerializeClass>
<creationTime>2013-11-25T09:53:25.3325289+05:30</creationTime>
<fileAction>Renamed</fileAction>
<Properties>
<FileAttributes fileName="validate json.txt">
<fileSize>307</fileSize>
<extension>.txt</extension>
<lastAccessTime>2013-11-25T09:53:25.3325289+05:30</lastAccessTime
<fullPath>C:\serialization\SerializationWithFileWatcher\SerializationWithFileWatcherProj\validate json.txt</fullPath>
</FileAttributes>
</Properties>
</SerializeClass>
</ArrayOfSerializeClass>
What I understand from the code above is that you are trying to extend the current XML, by first reading it as a FileStream and then using an XmlWriter to add some more content to it.
If my understanding is correct, then you are trying to write to the end of an existing XML file, which is not allowed since any XML document can have only one root node. In your case, that root node is ArrayOfSerializeClass.
So, in order to successfully achieve your task, you must append your XML within the root node.
Update:
Possible solution here: how to append a xml file in c#?