将xml转换为数据集

时间:2012-06-18 07:32:55

标签: c# xml dataset

当我尝试在数据集中转换xml隔离存储文件时,我得到一个例外,例如“无法访问,因为被其他用户使用”

我的代码:

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);

1 个答案:

答案 0 :(得分:1)

您似乎没有正确处置IDisposable资源。您应该始终使用语句将它们包装起来,以确保您没有泄漏句柄:

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);
}