【发布时间】:2009-08-28 02:17:23
【问题描述】:
我编写了一个 C# Winforms 程序来获取一个 XML 文件并将值从一个文本文件加载到用户在 UI 上指定的该字段的每个匹配项中。无论出于何种原因,程序都会在任何不包含值的节点上插入回车符。例如,它会对<example></example> 执行此操作,而不会对<country>USA</country> 之类的行为不端
是什么导致它这样做,我该如何防止它?这是处理此功能的部分的代码。
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);
【问题讨论】:
标签: c# xml winforms parsing text