sidd2004k
sidd2004k

Reputation: 49

convert xml into dataset

When I tried to convert a xml isolated storage file in dataset I get an exception like "cannot access,because being used by another user"

My code:

IsolatedStorageFile isfInsuranceFirm = null;

isfInsuranceFirm = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain, null, null);
Stream stream1 = new IsolatedStorageFileStream("PageAccess.xml",FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, isfInsuranceFirm);

stream1.Position = 0;

string path1 = stream1.GetType().GetField("m_FullPath", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(stream1).ToString();
string path2 = stream2.GetType().GetField("m_FullPath", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(stream2).ToString();

XmlDataDocument doc = new XmlDataDocument();
//doc.LoadXml(path1.Substring(path1.IndexOf('/') + 1));
doc.Load(path1);

Upvotes: 0

Views: 520

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

You don't seem to be properly disposing IDisposable resources. You should always wrap them in using statements to ensure that you are not leaking handles:

using (IsolatedStorageFile isfInsuranceFirm = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain, null, null))
using (Stream stream1 = new IsolatedStorageFileStream("PageAccess.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, isfInsuranceFirm))
{

    stream1.Position = 0;

    string path1 = stream1.GetType().GetField("m_FullPath", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(stream1).ToString();
    string path2 = stream2.GetType().GetField("m_FullPath", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(stream2).ToString();

    XmlDataDocument doc = new XmlDataDocument();
    //doc.LoadXml(path1.Substring(path1.IndexOf('/') + 1));
    doc.Load(path1);
}

Upvotes: 1

Related Questions