I am trying to create a program that will search for xml files for nodes in the form <disp-formula id="deqnX-Y">
, create a dictionary where the keys look like rid="deqnX" ... rid="deqnY"
, (where X increases by +1 until it reaches Y ), and their corresponding value mappings are similar to rid="deqnX-Y"
each. Then I can just do a search and replace with a dictionary to change the link nodes. that is, if the file has nodes such as <disp-formula id="deqn5-7">, <disp-formula id="deqn9-11">, <disp-formula id="deqn3a-3c">, <disp-formula id="deqn4p-5b">
, and there are link nodes in the form
<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">
which should be changed to
<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">
I am using the code below
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)
{
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;
}
, , <disp-formula id="deqn5-7c">
<disp-formula id="deqn2a-4">
. , , System.IO.IOException: . .
, , , <disp-formula id="deqn5-7c">
/ <disp-formula id="deqn2a-4">
, ?