【问题标题】:convert xml into dataset将xml转换为数据集
【发布时间】:2012-06-18 07:32:54
【问题描述】:

当我尝试转换数据集中的 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);

【问题讨论】:

    标签: c# xml dataset


    【解决方案1】:

    您似乎没有正确处理IDisposable 资源。您应该始终将它们包装在 using 语句中,以确保您不会泄漏句柄:

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

    【讨论】:

      猜你喜欢
      • 2012-01-13
      • 1970-01-01
      • 1970-01-01
      • 2011-01-06
      • 1970-01-01
      • 2011-09-07
      • 2022-01-20
      • 2021-03-25
      相关资源
      最近更新 更多