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

      

  • 相关阅读:
    2016701010126 2016-2017-2《java程序设计》集合
    201671010126 2016-2017-2《Java程序设计》第六周
    201671010126 2016-2017-2《Java程序设计》总结
    201671010128 2017-12-17《Java程序设计》之并发
    201671010128 2017-11-10《Java程序设计》之应用程序部署(2)
    201671010128 2017-11-29《Java程序设计》之应用程序部署
    201671010128 2017-11-29《Java程序设计》之Swing用户界面组件
    201671010128 2017-11-19《Java程序设计》之事件处理技术
    201671010128 2017-11-12《Java程序设计》之图形程序设计
    201671010128 2017-11-05《Java程序设计》之集合
  • 原文地址:https://www.cnblogs.com/ioriwellings/p/12048810.html
Copyright © 2020-2023  润新知