C# 使用LINQ从XML文件中删除所有节点时出现的问题

C# 使用LINQ从XML文件中删除所有节点时出现的问题,c#,linq-to-xml,C#,Linq To Xml,我正在尝试从XML文件中删除所有节点。但是它也删除了根节点的open标记 输入: <?xml version="1.0" encoding="utf-8" standalone="no"?> <!--Log the error count and error message--> <root> <ErrData> <Count>1</Count> <Timestamp>20

我正在尝试从XML文件中删除所有节点。但是它也删除了根节点的open标记

输入:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!--Log the error count and error message-->
<root>
    <ErrData>
        <Count>1</Count>
        <Timestamp>2011-11-21T11:57:12.3539044-05:00</Timestamp>
     </ErrData>
     <ErrData>max of 20 ErrData elements</ErrData>
 </root>
代码:

下面是我正在使用的代码,概念是将错误计数和时间戳记录到XML文件中。一旦达到阈值,将通过函数发送电子邮件,并从XML中删除所有节点。然后,当下一个错误出现时,它将开始输入xml文件,如下所示

XDocument doc = null;
XElement el;
if (!System.IO.File.Exists(path))
{

    doc = new XDocument(new XDeclaration("1.0", "utf-8", "no"));
    el = new XElement("root");
    //el = new XElement("root");
    XComment comment = new XComment("Log the error count and error message");
    doc.Add(comment);
}
else
{
    doc = XDocument.Load(path);
}
XElement p1 = new XElement("ErrData");
XElement p1Count = new XElement("Count", eventCount);
XElement p1Windowsatrt = new XElement("Timestamp", windowStart);

p1.Add(p1Count );
p1.Add(p1Windowsatrt );

if (doc.Root != null)
{
    el = doc.Root;
    el.Add(p1);
}
else
{
    el = new XElement("root");
    el.Add(p1);
}


try
{
    doc.Add(el);//Line throwing the exeception

}
catch (Exception e)
{

}
finally
{
    doc.Save(path);
}

使用
docs.Root.Nodes().Remove()

对于没有内容的标记(自动关闭标记)是有效的XML。如果您确实需要一个开始和结束标记,那么您需要在根节点中添加一些内容,如注释或文本。

第一句话中就出现了混淆:“我正在尝试从XML文件中删除所有节点/元素。”是哪一个?要删除所有节点还是所有元素

XML中有五种类型的节点:元素、文本、注释、处理指令和属性。如果您在这里交替使用“node”和“element”,那么您在使用XML时会遇到很多麻烦

您得到的,
是删除所有子代节点的代码的正确输出:它是一个名为
root
的元素,没有任何内容

你所期待的

<root>
</root>


是一个名为
root
的单个元素,其中包含包含空格的子文本节点,可能是换行符。您编写的代码删除了所有子代节点,而不仅仅是子代元素节点,因此它也删除了此文本节点。

是否确实缺少开头标记并且它不仅仅缩写为?这是我的错误,是缩写的根标记。但问题是,当下次使用同一个文件写入错误时,它抛出“此操作将创建一个结构不正确的文档”。我从这个错误开始,但忽略了关闭一个d abbreavated标记。您能发布尝试插入新
ErrData
元素的代码吗(在您得到异常的地方)?如-如果您将result.xml(低于实际OP)与缩写标记一起使用此代码(
docs.subjections(“ErrData”).Remove();
)你有例外吗?还是其他原因造成的?我将花更多时间深入学习LINQ概念。谢谢。这些是XML概念,不是LINQ概念。如果使用
XmlDocument
方法执行此操作,则会遇到相同的问题。
XDocument docs = XDocument.Load(path);
try
{                   
    docs.Descendants("ErrData").Remove();
}
XDocument doc = null;
XElement el;
if (!System.IO.File.Exists(path))
{

    doc = new XDocument(new XDeclaration("1.0", "utf-8", "no"));
    el = new XElement("root");
    //el = new XElement("root");
    XComment comment = new XComment("Log the error count and error message");
    doc.Add(comment);
}
else
{
    doc = XDocument.Load(path);
}
XElement p1 = new XElement("ErrData");
XElement p1Count = new XElement("Count", eventCount);
XElement p1Windowsatrt = new XElement("Timestamp", windowStart);

p1.Add(p1Count );
p1.Add(p1Windowsatrt );

if (doc.Root != null)
{
    el = doc.Root;
    el.Add(p1);
}
else
{
    el = new XElement("root");
    el.Add(p1);
}


try
{
    doc.Add(el);//Line throwing the exeception

}
catch (Exception e)
{

}
finally
{
    doc.Save(path);
}
<root>
</root>