C# 节点属性值替换程序获取异常?
我正在尝试创建一个程序,它将在xml文件中搜索C# 节点属性值替换程序获取异常?,c#,C#,我正在尝试创建一个程序,它将在xml文件中搜索格式的节点,创建一个字典,其中的键类似于rid=“deqnX”。。。rid=“deqnY”,(其中,X增加+1,直到它达到Y),它们各自的对应值类似于rid=“deqnX-Y”,然后我可以使用字典进行搜索和替换,以更改链接节点。i、 e.如果文件具有类似,的节点,并且表单中存在链接节点 <xref ref-type="disp-formula" rid="deqn5"> <xref ref-type="disp-formula" r
格式的节点,创建一个字典,其中的键类似于rid=“deqnX”。。。rid=“deqnY”
,(其中,X增加+1,直到它达到Y),它们各自的对应值类似于rid=“deqnX-Y”
,然后我可以使用字典进行搜索和替换,以更改链接节点。i、 e.如果文件具有类似,
的节点,并且表单中存在链接节点
<xref ref-type="disp-formula" rid="deqn5">
<xref ref-type="disp-formula" rid="deqn6">
<xref ref-type="disp-formula" rid="deqn10">
<xref ref-type="disp-formula" rid="deqn5c">
哪个应该改成
<xref ref-type="disp-formula" rid="deqn5-7">
<xref ref-type="disp-formula" rid="deqn5-7">
<xref ref-type="disp-formula" rid="deqn9-11">
<xref ref-type="disp-formula" rid="deqn4p-5b">
我现在正在使用下面的代码
void Button1Click(object sender, EventArgs e)
{
string active_filename = "";
string DirectoriesName = textBox1.Text;
string[] path = Directory.GetDirectories(DirectoriesName, "xml", SearchOption.AllDirectories)
.SelectMany(x => Directory.GetFiles(x, "*.xml", SearchOption.AllDirectories)).ToArray();
Dictionary<string, string> dict = new Dictionary<string, string> ();
var re = new Regex(@"deqn(\w+)-(\w+)");
foreach (var file in path)
{
dict.Clear();
active_filename = file;
XDocument doc = XDocument.Load(file, LoadOptions.PreserveWhitespace);
IEnumerable<XAttribute> list_of_elements = doc.Descendants("disp-formula").Where(z => (z.Attribute("id") != null) && re.IsMatch(z.Attribute("id").Value)).Attributes("id");
foreach (XAttribute ele in list_of_elements)
{
int from = 0, to = 0;
string strform = re.Match(ele.Value).Groups[1].Value;
string strTo = re.Match(ele.Value).Groups[2].Value;
Boolean bfrom = int.TryParse(strform,out from);
Boolean bto = int.TryParse(strTo,out to);
if (bfrom && bto)
{
for (int i = from; i <= to; i++)
dict.Add("rid=\"deqn" + i + "\"", "rid=\"" + ele.Value + "\"");
}
else {
for (int i = base36toInt(strform); i <= base36toInt(strTo); i++)
{
int temp = 0;
if (!int.TryParse(IntTo36Base(i), out temp))
{
dict.Add("rid=\"deqn" + IntTo36Base(i) + "\"", "rid=\"" + ele.Value + "\"");
}
}
}
foreach (KeyValuePair<string, string> element in dict)
{
//do a search all replace all (search Key and replace by Value
string text = File.ReadAllText(file);
text = text.Replace(element.Key, element.Value);
File.WriteAllText(file, text);
}
}
}
MessageBox.Show("Done");
}
public static int base36toInt(string s)
{
char[] baseChars = "0123456789abcdefghijklmnopqrstuvwxyz".ToCharArray();
char[] target = s.ToCharArray();
double result = 0;
for (int i = 0; i < target.Length; i++)
{
result += Array.IndexOf(baseChars, target[i]) * Math.Pow(baseChars.Length, target.Length - i - 1);
}
return Convert.ToInt32(result);
}
public static string IntTo36Base(int value)
{
char[] baseChars = "0123456789abcdefghijklmnopqrstuvwxyz".ToCharArray();
string result = string.Empty;
int targetBase = baseChars.Length;
do
{
result = baseChars[value % targetBase] + result;
value = value / targetBase;
}
while (value > 0);
return result;
}
void按钮1单击(对象发送者,事件参数e)
{
字符串活动_filename=“”;
字符串DirectoriesName=textBox1.Text;
string[]path=Directory.GetDirectories(DirectoriesName,“xml”,SearchOption.AllDirectories)
.SelectMany(x=>Directory.GetFiles(x,“*.xml”,SearchOption.AllDirectories)).ToArray();
Dictionary dict=新字典();
var re=新正则表达式(@“deqn(\w+)-(\w+));
foreach(路径中的var文件)
{
格言(Clear);
活动文件名=文件;
XDocument doc=XDocument.Load(文件,LoadOptions.PreserveWhitespace);
元素的IEnumerable列表=文档子体(“disp公式”)。其中(z=>(z.Attribute(“id”)!=null)和&re.IsMatch(z.Attribute(“id”).Value))。属性(“id”);
foreach(元素列表中的XAttribute元素)
{
int from=0,to=0;
字符串strform=re.Match(ele.Value).Groups[1].Value;
字符串strTo=re.Match(ele.Value).Groups[2].Value;
布尔bfrom=int.TryParse(strform,out-from);
布尔bto=int.TryParse(strTo,out-to);
如果(从&B到&B)
{
对于(int i=from;i好的,这可能是一个愚蠢的答案,但是……你有没有想过改变你的正则表达式?我的意思是,现在,它将匹配“deqn”,后面是字母数字字符流,后面是“-”,后面是另一个字母数字流。因此,即使是像“deqnasdf zxcv”这样的东西也会适合
我建议将其改为:“deqn(\d+)-(\d+)”—又名,将“任意字母数字”改为“任意数字”.我的意思是,如果你想跳过像deqn1-2c这样的东西,这将阻止它们甚至出现在比赛中。此外,你越是缩小正则表达式的范围,你就越有可能在未来的比赛中遇到你没有计划的错误
我得到:
System.IO.IOException:无法对打开了用户映射节的文件执行请求的操作
看起来你想要更改的文件正在使用,所以你不能更改它。因为你说有时会发生这种情况,所以我猜你的循环正在引发错误。它会快速打开和关闭文件,而你可以在循环外打开文件并在循环后写入。无论如何,当你遇到上述错误时,你可以找到正在使用该文件的应用程序by一个程序,如
测试它:
不要使用此代码:
foreach (KeyValuePair<string, string> element in dict)
{
//do a search all replace all (search Key and replace by Value
string text = File.ReadAllText(file);
text = text.Replace(element.Key, element.Value);
File.WriteAllText(file, text);
}
foreach(dict中的KeyValuePair元素)
{
//执行搜索全部替换全部(搜索键和替换值
string text=File.ReadAllText(文件);
text=text.Replace(element.Key,element.Value);
WriteAllText(文件,文本);
}
使用这个:
string text = File.ReadAllText(file);
foreach (KeyValuePair<string, string> element in dict)
{
//do a search all replace all (search Key and replace by Value
text = text.Replace(element.Key, element.Value);
}
File.WriteAllText(file, text);
string text=File.ReadAllText(文件);
foreach(dict中的KeyValuePair元素)
{
//执行搜索全部替换全部(搜索键和替换值
text=text.Replace(element.Key,element.Value);
}
WriteAllText(文件,文本);
Get sysinternals的可能重复项并检查文件锁定。这将告诉您是什么锁定了您的文件。@您不需要只避免这两个节点吗?我想您的问题在于//执行搜索全部替换全部(搜索键并替换为值字符串text=file.ReadAllText(文件);text=text.Replace(element.Key,element.Value);File.writealText(File,text);如果已使用XDocument打开文件,则无法打开/写入该文件