• XmlDocument.load 读文件报异常:文件正被其它线程使用,解决方法


    string filePath = Form1.getProjectFilePath();
                System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
                //xmlDoc.Load(filePath);
                using (System.IO.FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    using (System.IO.StreamReader sr = new StreamReader(fs, Encoding.GetEncoding(936)))
                    {
                        var strXml = sr.ReadToEnd();
                        xmlDoc.LoadXml(strXml);
                    }
                }
    protected void abcBankPayment(string no, string amount, string productName, string qcode)
            {
                var buildingName = productName.Split(':')[0];
                var bean = new ABCBankAccountBean();
                DataContractJsonSerializer jsonSerial = new DataContractJsonSerializer(typeof(ABCBankAccountBean));
                var result = File.ReadAllText(MapPath("~/Finance/ABCBankAccountMaping.json"), System.Text.Encoding.UTF8);
                using (MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(result)))
                {
                    bean = (ABCBankAccountBean)jsonSerial.ReadObject(stream);
                }
                Building entityBuilding = bean.buildingItems.Where(exp => exp.buildingNames.Contains(buildingName)).FirstOrDefault();
                if (entityBuilding == null)
                {
                    //楼盘没有农行支付配置
                    getMsgLabel().Text = string.Format("楼盘'{0}'不能使用农行接口支付", buildingName);
                    return;
                }
    }

    string[] arrayXmlNodeName_01 = new string[]
    {
    "YHX_XC_H", "YHX_XC_H1", "YHX_XC_DH", "YHX_XC_NDJ","YHX_XC_H2", "YHX_XC_H3", "YHX_XC_H4",
    "YHX_XC_W", "YHX_XC_W2", "YHX_XC_L1", "YHX_XC_L2", "YHX_XC_L3"
    };

    string[][] arrayXmlNodeName_02 = new string[][]
    {
    new string[] { "YHX_XC_SHB_MAT", "YHX_XC_SHB_T" },
    new string[] { "YHX_XC_LB_MAT", "YHX_XC_LB_T" },
    new string[] { "YHX_XC_BB_MAT", "YHX_XC_BB_T" },
    new string[] { "YHX_XC_LGJB_MAT", "YHX_XC_LGJB_T" },
    new string[] { "YHX_XC_LGDB_MAT", "YHX_XC_LGDB_T" },
    new string[] { "YHX_XC_XHB_MAT", "YHX_XC_XHB_T" }
    };


    string filePath = Form1.getProjectFilePath(); System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument(); //xmlDoc.Load(filePath); using (System.IO.FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (System.IO.StreamReader sr = new StreamReader(fs, Encoding.GetEncoding(936))) { var strXml = sr.ReadToEnd(); xmlDoc.LoadXml(strXml); } } var nodeRoot = xmlDoc.SelectSingleNode("/Root/xc"); nodeRoot.RemoveAll(); Action<string> action = delegate (string ele) { var nodeTemp = xmlDoc.CreateElement(ele); Control control = this.Controls.Find(ele, true)[0]; if (control.GetType() == typeof(ComboBox)) { nodeTemp.InnerText = (control as ComboBox).SelectedValue.ToString(); } else { nodeTemp.InnerText = control.Text; } nodeRoot.AppendChild(nodeTemp); }; Action<string[]> action2 = delegate (string[] ele) { Array.ForEach<string>(ele, action); }; Array.ForEach<string>(arrayXmlNodeName_01, action); Array.ForEach<string[]>(arrayXmlNodeName_02, action2); xmlDoc.Save(filePath); MessageBox.Show("保存成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);

      

  • 相关阅读:
    BZOJ2809 dispatching
    BZOJ1486 最小圈
    BZOJ1096 仓库建设
    BZOJ3190 赛车
    BZOJ1911 特别行动队
    BZOJ1202 狡猾的商人
    BZOJ1007 水平可见直线
    BZOJ2150 部落战争
    如何用PHP遍历文件数目 或删除目录下的全部文件?
    php对文件/目录操作的基础知识(图解)
  • 原文地址:https://www.cnblogs.com/ioriwellings/p/12048810.html
Copyright © 2020-2023  润新知