C# XML文件在每次运行中都会被覆盖
我正在尝试将一些数据写入XML文件。 实际上,我可以这样做,但在每次运行中,XML文件都会被覆盖,而我希望它添加另一行。 这就是我到目前为止所做的:C# XML文件在每次运行中都会被覆盖,c#,xml,selenium,C#,Xml,Selenium,我正在尝试将一些数据写入XML文件。 实际上,我可以这样做,但在每次运行中,XML文件都会被覆盖,而我希望它添加另一行。 这就是我到目前为止所做的: public static void StoreCustomerIntoXML(string Id) { string pth = System.Reflection.Assembly.GetCallingAssembly().CodeBase; string actualPath = pth.Subs
public static void StoreCustomerIntoXML(string Id)
{
string pth = System.Reflection.Assembly.GetCallingAssembly().CodeBase;
string actualPath = pth.Substring(0, pth.LastIndexOf("bin"));
string projectPath = new Uri(actualPath).LocalPath;
string reportPath = projectPath + "Customers\\CustomersListCreated.xml";
XmlDocument xmlDoc = new XmlDocument();
XmlNode rootNode = xmlDoc.CreateElement("Customers");
xmlDoc.AppendChild(rootNode);
XmlNode userNode = xmlDoc.CreateElement("Id");
userNode.InnerText = Id;
rootNode.AppendChild(userNode);
xmlDoc.Save(reportPath);
}
因此,第一次调用该方法将包括Id=1234
第二次运行将包括Id=6543
XML文件将始终包含上次运行的Id,并且仅包含此Id。请尝试此操作
public static void StoreCustomerIntoXML(string Id)
{
string pth = System.Reflection.Assembly.GetCallingAssembly().CodeBase;
string actualPath = pth.Substring(0, pth.LastIndexOf("bin"));
string projectPath = new Uri(actualPath).LocalPath;
string reportPath = projectPath + "CustomersListCreated.xml";
XmlDocument xmlDoc = new XmlDocument();
if (File.Exists(reportPath))
{
xmlDoc.Load(reportPath);
XmlNode rootNode = xmlDoc.DocumentElement;
xmlDoc.AppendChild(rootNode);
XmlElement elem = xmlDoc.CreateElement("Id");
elem.InnerText = Id;
rootNode.AppendChild(elem);
}
else
{
XmlNode rootNode = xmlDoc.CreateElement("Customers");
xmlDoc.AppendChild(rootNode);
XmlNode userNode = xmlDoc.CreateElement("Id");
userNode.InnerText = Id;
rootNode.AppendChild(userNode);
}
xmlDoc.Save(reportPath);
}
您可以检查文件是否存在,并使用该方法加载它,而不是每次调用方法时都创建一个新文件
public static void StoreCustomerIntoXML(string Id)
{
string pth = System.Reflection.Assembly.GetCallingAssembly().CodeBase;
string actualPath = pth.Substring(0, pth.LastIndexOf("bin"));
string projectPath = new Uri(actualPath).LocalPath;
string reportPath = projectPath + "Customers\\CustomersListCreated.xml";
XmlDocument xmlDoc;
if (File.Exists(reportPath))
xmlDoc = XDocument.Load(reportPath);
else
xmlDoc = new XmlDocument();
XmlNode rootNode = xmlDoc.CreateElement("Customers");
xmlDoc.AppendChild(rootNode);
XmlNode userNode = xmlDoc.CreateElement("Id");
userNode.InnerText = Id;
rootNode.AppendChild(userNode);
xmlDoc.Save(reportPath);
}
每次调用
StoreCustomerIntoXml
方法时,都会创建新的xml文件(在XmlDocument xmlDoc=new XmlDocument();
)这就是问题所在,但是我在方法内部可以做些什么来避免它,并添加新行而不是创建新文件?在StoreCustomerIntoXml
方法外部初始化xmlDoc
,并将其作为参数传递。创建日志文件时通常使用xml格式。通常会创建非标准xml。标准xml只有一个根标记。对于日志文件,最终在根级别会有多个标记。要创建标准xml文件,您必须在关闭标记之前添加新数据,这并不简单。所以大多数人只是在根级别使用带有多个标记的非标准格式。
public static void StoreCustomerIntoXML(string Id)
{
string pth = System.Reflection.Assembly.GetCallingAssembly().CodeBase;
string actualPath = pth.Substring(0, pth.LastIndexOf("bin"));
string projectPath = new Uri(actualPath).LocalPath;
string reportPath = projectPath + "Customers\\CustomersListCreated.xml";
XmlDocument xmlDoc = new XmlDocument();
XmlNode rootNode;
if (File.Exists(reportPath))
{
xmlDoc.Load(reportPath);
rootNode = xmlDoc.DocumentElement;
}
else
{
rootNode = xmlDoc.CreateElement("Customers");
xmlDoc.AppendChild(rootNode);
}
XmlNode userNode = xmlDoc.CreateElement("Id");
userNode.InnerText = Id;
rootNode.AppendChild(userNode);
xmlDoc.Save(reportPath);
}