积极答复者
xml正由另一进程使用,因此该进程无法访问该文?

问题
答案
-
您好,
这样加载是不会锁住这个文件,是不是写的时候没释放文件句柄但失败了,看看你的完整代码
问题要简单,错误须详细@错误/异常/堆栈信息+操作系统+软件版本+all the context of the issue Hope Helpful | http://www.leoworks.net- 已标记为答案 Csharpzero 2010年4月26日 5:48
全部回复
-
您好,
这样加载是不会锁住这个文件,是不是写的时候没释放文件句柄但失败了,看看你的完整代码
问题要简单,错误须详细@错误/异常/堆栈信息+操作系统+软件版本+all the context of the issue Hope Helpful | http://www.leoworks.net- 已标记为答案 Csharpzero 2010年4月26日 5:48
-
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");
value = angel.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的代码。。
大 家看看有什么问题? -
我在一个窗体上读取一个xml文件,然后直接上这个窗体上改 一个值保存到先前的xml文件中,老是提示“xml正由另一进程使用,因此该进程无法访问该文”,我应该怎么做?还是我的读写方法有问题。
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");
value = angel.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的代码。。
大 家看看有什么问题?- 已合并 Sheng Jiang 蒋晟Moderator 2010年4月26日 16:34
-
感谢Xiaofeng Wang <abbr class="affil"> 的提醒。。。
</abbr>自己搞定 。结贴了。。