Parsing a XML File and Replacing Chosen Node With Values From Text File
- by transmogrify
I wrote a C# Winforms program to take a XML file and load values from a text file into each occurrence of that field which the user specifies on the UI. For whatever reason, the program inserts a carriage return on any nodes which don't contain a value. For example, it will do that to <example></example> whereas it will not misbehave on something like <country>USA</country>
What is causing it to do this and how can I prevent it? Here is the code from the part which handles this functionality.
XmlDocument LoadXmlDoc = new XmlDocument();
StreamReader sr = File.OpenText(DataLoadTxtBx.Text);
string InputFromTxtFile;
LoadXmlDoc.Load(XmlPath.Text);
XmlNodeList NodeToCreateOrReplace = LoadXmlDoc.GetElementsByTagName(XmlTagNameTxtBx.Text);
foreach (XmlNode SelectedNode in NodeToCreateOrReplace)
{
if ((InputFromTxtFile = sr.ReadLine()) != null)
{
SelectedNode.InnerText = InputFromTxtFile;
}
}
sr.Close();
LoadXmlDoc.Save(XmlPath.Text);