A c# program that searches through multiple XML files and text to certain elements inside

Clash Royale CLAN TAG#URR8PPPA c# program that searches through multiple XML files and text to certain elements inside
I need to create a c# program that searches through multiple XML files and adds a post fix entered by the user to certain elements inside those XML files, those elements all have the string "_name" inside them, which is a blessing considering now I know what extra condition I can use to make sure I'm altering the correct elements, My basic understanding of how the process is gonna go is like this:
1- I let user browse through his computer and open the folder in which the XML files exist.
2- I let the program add "main" to the chosen path and so open the main.xml file which contains all the files I'll be needing to alter. (done)
3- create a loop that will open each file inside the main.xml file and store all its' contents inside a list of strings.
4- now compare each element inside the list with the content of each folder, and if a match is found, i make sure the name tags contain "%_Name" in them before making a change. since for example " bla bla May exist in more than one file and I'll be needing to change all of them.
Correct me if I'm wrong or if there's a better solution than this.
Here's the code I've written so far.
//The browse button.
private void button1_Click(object sender, EventArgs e) {
string folderPath = "";
FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) {
folderPath = folderBrowserDialog1.SelectedPath;
textBox1.Text = folderPath;
}
// the add text button click will open the main.xml and get all the names of other files inside from it. That's what I've written so far.
private void button2_Click(object sender, EventArgs e) {
if (textBox1.Text == null)
MessageBox.Show("Please choose a folder");
else {
string postfix = textBox2.Text;
if ((postfix.Length > 0) && (postfix.Length < 2)) {
// Load the document.
String folderPath = textBox1.Text;
XmlDocument doc = new XmlDocument();
doc.Load(folderPath + "\Main.xml");
int i = 0;
XmlNode root = doc.FirstChild.NextSibling;
XmlNodeList tree = root.ChildNodes;
List<string>mainList = new List<string>();
XmlReader reader = null;
foreach (XmlNode node in tree) {
string url = tree.Item(i).InnerText;
mainList.Add(tree.Item(i).InnerText);
string name = mainList[i];
reader = XmlReader.Create(url);
while (reader.Read()) {
if ((reader.NodeType == XmlNodeType.Element) && reader.Name == name) { }
}
i++;
}
}
else {
MessageBox.Show("Please enter atleast 1 character or 2 at maximum for the postfix.");
}
}
}
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
There is no reason to rename all the elements. First an xml can have an array of the same element name so there is no reason to change name to eliminate duplicates. If you need to indicate where a node came from then add a new attribute to the root element that indicates the source of the node.
– jdweng
3 hours ago