我在一个窗体上读取一个xml文件,然后直接上这个窗体上改一个值保存到先前的xml文件中,老是提示“xml正由另一进程使用,因此该进程无法访问该文”,我应该怎么做?还是我的读写方法有问题。我是用以下方法读写的。XmlDataDocument doc = new XmlDataDocument();doc.Load(“*.xml");doc.Save("*.xml");大家帮我想想啊。搞了几天了

解决方案 »

  1.   

    应该是你doc.Load(“*.xml");的时候正在使用这个xml,怎么解决期待高手
      

  2.   

    doc.Load(“*.xml")之后,你应该有操作XML吧?
    操作XML后有没有需要关闭文件流什么之类的东西,把全部代码帖出来。。
    sr.Close(); 
      fs.Close();
      

  3.   

     public static void WriteToXml(int i, string columnPos, string[] columnShows, Color startColor, Color endColor, Color theFontColor, Color selectFontColor, int angle, bool showGradient, Font theFont, string fistRowPos, string lineSpacing, string pageInfoPos, string rowsPerPage, string location, string formSize, string backGroundImage)
            {
                //iniListSettingArr();
                
                XmlDocument doc = new XmlDocument();             
                doc.Load(returnListSettingDir());
                //System.IO.FileStream fs = new System.IO.FileStream(returnListSettingDir(), FileMode.Open, FileAccess.Read,FileShare.ReadWrite);
                //doc.Load(fs);
                 
                 
                XmlNode RootNode = doc.SelectSingleNode(string.Format("/ListSetting/list[@ID={0}]", i));
                try
                {
                    XmlNodeList childNodes = RootNode.ChildNodes;
                    //foreach (XmlNode cNode in childNodes)
                    foreach (XmlNode cn in childNodes)
                    {
                        //XmlNodeType type = cNode.NodeType;
                        XmlElement cNode = (XmlElement)cn;
                        string value = ((XmlElement)cNode).GetAttribute("value");
                        if (cNode.Name == "ColumnPos")
                            value = columnPos;//转换过程出错,该如何处理???
                        if (cNode.Name == "ColumnShow")
                            //value = columnShows[];
                            if (cNode.Name == "StartColor")
                                value = startColor.ToString();
                        if (cNode.Name == "EndColor")
                            value = endColor.ToString();
                        if (cNode.Name == "FontColor")
                            value = theFontColor.ToString();
                        if (cNode.Name == "SelectColor")
                            value = selectFontColor.ToString();
                        if (cNode.Name == "Angle")
                        {
                            //cNode.SetAttribute("Angel","100");
                            cNode.SetAttribute("value", "20");
                            //value = "10";
                            //MessageBox.Show(value);
                            //MessageBox.Show(angle.ToString());                    }
                        if (cNode.Name == "ShowGradient")
                            value = showGradient.ToString();
                        if (cNode.Name == "TheFont")
                        {
                            //string[] ss = value.Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);
                            //listSettings[i].TheFont = new Font(ss[0], float.Parse(ss[1]), System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                            value = theFont.ToString();
                        }
                        if (cNode.Name == "FirstRowPos")
                            value = fistRowPos.ToString();
                        if (cNode.Name == "LineSpacing")
                            value = lineSpacing.ToString();
                        if (cNode.Name == "PageInfoPos")
                            value = pageInfoPos.ToString();
                        if (cNode.Name == "RowsPerPage")
                            value = rowsPerPage.ToString();
                        if (cNode.Name == "Location")
                            value = location.ToString();
                        if (cNode.Name == "Size")
                            value = formSize.ToString();
                        if (cNode.Name == "BackgroundImage")
                            value = backGroundImage.ToString();
                        //if (cNode.Name == "ImgTrue") listSettings[i].ImgTrue = Image.FromFile(returnPictureDir(value));
                        //if (cNode.Name == "ImgFalse") listSettings[i].ImgFalse = Image.FromFile(returnPictureDir(value));                    
                    }
                  
                }
                catch (Exception e)
                {
                    System.Windows.Forms.MessageBox.Show(string.Format("写入listSetting.xml时出错错误信息是:{1}.\r\n错在id={0}段,请检查并修正该文件", (MyListID)i, e.Message));
                }                      
                
                doc.Save(returnListSettingDir());            }
    这是写入xml的代码。。
    大家看看有什么问题?
      

  4.   


    知道怎么回事了。感谢大家的帮助。感谢lee_b的提醒。。