Parsing a XML File and Replacing Chosen Node With Values From Text File

Posted by transmogrify on Stack Overflow See other posts from Stack Overflow or by transmogrify
Published on 2009-08-28T02:17:23Z Indexed on 2010/04/09 23:53 UTC
Read the original article Hit count: 379

Filed under:
|
|
|
|

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);

© Stack Overflow or respective owner

Related posts about c#

Related posts about winforms