Below is my sample XML file stored in the server ;
<exam>
<name>Maths</name>
<percentage>100</percentage>
</exam>
<exam>
<name>Physics</name>
<percentage>70</percentage>
</exam>
<exam>
<name>Chemistry</name>
<percentage>70</percentage>
</exam>
I have another table as mentioned below
Name of Exam Percentage
Maths 50
Physics 60
Chemistry 70
What I need here is that I need to read this XML File and replace the percentage value in the XML file based on the table that I have. I have more than 75 tags for exam
I have used the below logic of hardcoding everything but I am not sure if my logic would be good
public static void Changepercentage()
{
try{
string xmlpercentage= Loaddefault.xmlpercentage;
string f = xml
List<string> lines = new List<string>();
// 2
// Use using StreamReader for disposing.
using (StreamReader r = new StreamReader(f, System.Text.Encoding.Default))
{
// 3
// Use while != null pattern for loop
string line;
while ((line = r.ReadLine()) != null)
{
if (System.Text.RegularExpressions.Regex.IsMatch(line, "<exam>Maths</exam>"))
{
lines.Add(@"" + line + "");
line = "<percentage>50</percentage>";
}
}
}
System.IO.File.WriteAllLines(xmlpercentage, lines.ToArray());
Logger.Instance.InfoLog("Successfully updated the percentage.xml file");
}
catch (Exception ex)
{
Logger.Instance.ErrorLog("Problem in updating the percentage.xml file :"+ ex.Message);
throw new Exception("Problem in updating the percentage.xml file");
}
}