Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 有没有一种方法可以在不使用XDocument的情况下将新的xml数据附加到xml文件中已经存在的xml中?_C#_Xml_Linq_Linq To Xml - Fatal编程技术网

C# 有没有一种方法可以在不使用XDocument的情况下将新的xml数据附加到xml文件中已经存在的xml中?

C# 有没有一种方法可以在不使用XDocument的情况下将新的xml数据附加到xml文件中已经存在的xml中?,c#,xml,linq,linq-to-xml,C#,Xml,Linq,Linq To Xml,通常,我将日志消息写入xml文件。我使用Linq的XDocument 当xml日志变大时,我通常会有无响应的UI,因此我正在寻找一种替代方法来实现相同的结果 下面是我如何使用Linq登录到xml文件的摘录 // Create the xml file if it does not exist if (!File.Exists(_filePath)) { _xmlDocument = new XDocument(new XDeclaration("1.0", "

通常,我将日志消息写入xml文件。我使用
Linq
XDocument

当xml日志变大时,我通常会有无响应的UI,因此我正在寻找一种替代方法来实现相同的结果

下面是我如何使用
Linq
登录到xml文件的摘录

// Create the xml file if it does not exist
if (!File.Exists(_filePath))
{
    _xmlDocument = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"),
        new XElement("Logs"));

    _xmlDocument.Save(_filePath);
}

// Load the xml file
_xmlDocument = XDocument.Load(_filePath);

// Add new log to the logs
_xmlDocument?.Element("Logs")?
    .AddFirst(new XElement($"{logLevel.ToString()}",
              new XAttribute("Date", DateTime.Now.ToString("g")),
              new XAttribute("Filepath", $"{values?[1]}"),
                  new XElement("Exception", new XAttribute("EventId", eventId), exception.Source),
                               new XElement("Message",
                               new XAttribute("Origin", $"{values?[0]}"),
                               new XAttribute("LineNumber", $"{values?[2]}"),
                               $"{values?[3]}")
                               )
              );

            // Save the document
            _xmlDocument.Save(_filePath);

您可以尝试将Xml文件当作数据表使用,插入一条记录,然后再次写入该文件。 例如:

        DataSet oDataset = new DataSet();
        oDataset.ReadXml(yourfile);
        DataTable dt = oDataset.Tables["yourtable"];
        //Insert a record
        oDataset.WriteXml(yourfile);

一种技术是像这样维护日志文件

<log-entry.../>
<log-entry.../>
<log-entry.../>

没有封闭的包装器元素。这意味着您可以使用普通的文件附加方法对其进行附加。然后,如果要将其作为XML读取,请将其作为另一个文件中的外部实体引用:

<!DOCTYPE log [
<!ENTITY e SYSTEM "logdata.xml">
]>
<log>&e;</log>

&e;
在读取文件时,您仍然需要承担解析的全部成本,但现在可以直接且廉价地附加到文件中


当然,缺点是现在的安全人员对外部实体很偏执。

有两件事:1)您正在使用.AddFirst,它在顶部插入条目,这意味着许多条目必须随着时间的推移而移动。AppendChild的性能会更好。2) 避免使用?。您已经确保了文档的可用性。我希望新日志始终保持在顶部。您能给我指一下有助于实现您建议的技术的参考资料吗?一本关于XML的好书,如Bob du Charme,告诉您需要了解的关于实体的一切。就我个人而言,我很少使用XML实体,但偶尔会出现这样的问题,因为它们非常方便。这种方法是否有值得关注的折衷之处?我多年来一直使用它来管理配置文件,也就是曾经的.ini文件,从来没有出现过问题。