patterncsharpMinor
Using XDocument properly
Viewed 0 times
properlyusingxdocument
Problem
I have a WCF service in which I am reading and adding some records to a .xml file. I load the .xml document in the constructor and use it in all the methods and save when it's updated.
I want to know if this is the correct way of using it. I have seen code around where people load the
I want to know if this is the correct way of using it. I have seen code around where people load the
xDocument only when it's required. If I try that, will it affect performance of individual method calls in cases of big .xml files? [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class MyService : IMyService
{
private readonly string mSchemaPath = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "App_Data",
"schema_0.1.xsd");
private readonly string mXmlPath = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "App_Data",
"MyDataRecords.xml");
private XDocument mXDocument;
public MyService()
{
try
{
//load xml document
mXDocument = XDocument.Load(mXmlPath);
if (mXDocument == null)
{
throw new Exception("Null returned while reading xml file");
}
}
catch (Exception e)
{
//my exception management code
}
}
public List GetAllRecords()
{
////fetch records from xDocument
mXDocument.Save();
}
public void AddRecord(MyRecord record)
{
////add record
mXDocument.Save();
}Solution
As you are using a singleton which loads the document once, I would say "Yes you will probably see some degradation in performance to service calls if you only load it on demand".
If your service is the one and only program which modifies the document then your approach is probably viable.
A few notes:
-
Don't throw
-
You mentioned that the file might be big. I don't know what the particular requirements of your application are but be aware that should the service crash at the right moment while saving changes you'll very likely be left with a corrupt xml document because it was only partially saved. Even if it is only a one a million chance it can still be a big problem.
-
Your service sounds like it's using an xml file as some kind of cheap db backend data store. While there might be scenarios where this is the best/only option forward you might want to consider if it's not better to use an embedded db like SqLite, Sql Server CE or similar ones and have an xml export option instead.
If your service is the one and only program which modifies the document then your approach is probably viable.
A few notes:
-
Don't throw
Exception. Be as specific as you can.-
You mentioned that the file might be big. I don't know what the particular requirements of your application are but be aware that should the service crash at the right moment while saving changes you'll very likely be left with a corrupt xml document because it was only partially saved. Even if it is only a one a million chance it can still be a big problem.
-
Your service sounds like it's using an xml file as some kind of cheap db backend data store. While there might be scenarios where this is the best/only option forward you might want to consider if it's not better to use an embedded db like SqLite, Sql Server CE or similar ones and have an xml export option instead.
Context
StackExchange Code Review Q#55956, answer score: 3
Revisions (0)
No revisions yet.