I'm trying to programmatically replace certain words and characters in a plain text file from a predefined list in an XML file. Everything is written into a class named PlayImport while the list of discountable phrases is in the Discount class, original script below:
public class Discount //import all discountable elements from an xml file I wrote
{
public List<string> Punctuation = new List<string>();
public List<string> Words = new List<string>();
public Discount(string path)
{
XmlDocument Reader = new XmlDocument();
Reader.Load(path);
foreach (XmlNode xmlNode in Reader.DocumentElement.ChildNodes[0].ChildNodes)
this.Punctuation.Add($" {Convert.ToString(xmlNode.Attributes["Name"].Value)} ");
foreach (XmlNode xmlNode in Reader.DocumentElement.ChildNodes[1].ChildNodes)
this.Words.Add($" {Convert.ToString(xmlNode.Attributes["Name"].Value)} ");
}
}
public class PlayImport //gather data on a play and import the text file of the play
{
public string Title;
public int Year;
public string Location;
public List<string> Cast = new List<string>();
public List<Counter> WordCount = new List<Counter>();
public string text;
public PlayImport(string path, int Num) //gather info from an index.xml file I wrote
{
XmlDocument Reader = new XmlDocument();
Reader.Load(path);
this.Title = Convert.ToString(Reader.DocumentElement.ChildNodes[Num].Attributes["Title"].Value);
this.Year = Convert.ToInt32(Reader.DocumentElement.ChildNodes[Num].Attributes["Year"].Value);
this.Location = Convert.ToString(Reader.DocumentElement.ChildNodes[Num].Attributes["Location"].Value);
foreach (XmlNode xmlNode in Reader.DocumentElement.ChildNodes[Num].ChildNodes[0].ChildNodes)
this.Cast.Add(Convert.ToString(xmlNode.Attributes["Name"].Value));
}
public void ImportText(Discount discounts) //pass the Discount object to be used in the function
{
string newline;
StreamReader Reader = new StreamReader(this.Location);
foreach (char line in Reader.ReadToEnd())
{
newline = line.ToString();
foreach (string i in discounts.Punctuation)
{
newline.Replace(i, "");
}
this.text += newline;
}
Reader.Close();
Console.Write(text); //checking it worked
}
}
In the PlayImport.ImportText function, I load the txt file contents into this.text variable which I then would like to manipulate to remove the required words and punctuation (here though im just focusing on puntuation). I thought perhaps the issue was in my nested loops so I rewrote the script slightly:
public void ImportText(Discount discounts)
{
StreamReader Reader = new StreamReader(this.Location);
foreach (char line in Reader.ReadToEnd())
{
this.text += line;
}
Reader.Close();
for (int i = 0; i > discounts.Punctuation.Count; i++)
{
this.text.Replace(discounts.Punctuation[i], "");
}
Console.Write(this.text); //checking it worked
}
note: I tried using a for loop here as well as a foreach loop, but neither worked, so I decided to go manual with this script:
public void ImportText(Discount discounts)
{
StreamReader Reader = new StreamReader(this.Location);
foreach (char line in Reader.ReadToEnd())
{
this.text += line;
}
Reader.Close();
this.text.Replace(@",", " ");
this.text.Replace(@".", " ");
this.text.Replace(@":", " ");
this.text.Replace(@";", " ");
this.text.Replace(@"!", " ");
this.text.Replace(@"?", " ");
this.text.Replace(@"'", " ");
this.text.Replace(@"/", " ");
this.text.Replace(@"(", " ");
this.text.Replace(@")", " ");
Console.Write(this.text); //checking it worked
}
Which still doesnt work, leaving me to think that perhaps the problem lies either in this.text itself or the string.remove function as I'm trying to use it here. Can anyone help me figure out what the problem is and how it can be fixed?