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

发布于 2024-12-17 12:52:55 字数 1892 浏览 1 评论 0原文

我正在尝试从 XML 文件中删除所有节点。但它也删除了根节点打开标记。使用 C# anf Linq

输入:

<?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 version="1.0" encoding="utf-8" standalone="no"?>
<!--Log the error count and error message-->
<root>
</root>

实际操作:编辑

<?xml version="1.0" encoding="utf-8" standalone="no"?>
    <!--Log the error count and error message-->
<root />

代码:

XDocument docs = XDocument.Load(path);
try
{                   
    docs.Descendants("ErrData").Remove();
}

代码:

下面是我正在使用的代码,概念是错误计数和时间戳被记录到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);
}

I am trying to remove all the nodes from the XML file. But it's removing the root node open tag also .Using C# anf Linq

Input:

<?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>

Expected OP:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!--Log the error count and error message-->
<root>
</root>

Actual OP:EDITED

<?xml version="1.0" encoding="utf-8" standalone="no"?>
    <!--Log the error count and error message-->
<root />

Code :

XDocument docs = XDocument.Load(path);
try
{                   
    docs.Descendants("ErrData").Remove();
}

CODE:

Below is the code i am using ,the concept is the error count and timestamp are logged into XML file.Once its reached the threshold value,email will be send by function and remove all the nodes from the xml. Then when the next error comes it will start entering in to the xml file as below,

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白首有我共你 2024-12-24 12:52:55

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

Use docs.Root.Nodes().Remove().

不必在意 2024-12-24 12:52:55

是没有内容的标签(自闭合标签)的有效 XML。如果您绝对需要开始和结束标记,则需要在根节点中放置一些内容,例如注释或文本。

<root /> is valid XML for a tag with no content (self-closing tags). If you absolutely need an opening and closing tag you need to put some content in the root node like a comment or text.

雨的味道风的声音 2024-12-24 12:52:55

混乱在于您的第一句话:“我正在尝试从 XML 文件中删除所有节点/元素。”是哪一个?您想删除所有节点还是所有元素?

XML中有五种类型的节点:元素、文本、注释、处理指令和属性。如果您交替使用“节点”和“元素”,就像您在这里一样,那么您在使用 XML 时将会遇到无穷无尽的麻烦。

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

您所期望的

<root>
</root>

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

The confusion is in your very first sentence: "I am trying to remove all the nodes/elements from the XML file." Which one is it? Do you want to remove all nodes, or all elements?

There are five types of nodes in XML: elements, text, comments, processing instructions, and attributes. If you use "node" and "element" interchangeably, as you are here, you're going to have no end of trouble working with XML.

What you got, <root/>, is the correct output for code that removes all descendant nodes: it's a single element named root with no content.

What you expect,

<root>
</root>

is a single element named root that contains a child text node containing whitespace, probably a newline. The code you wrote removes all descendant nodes, not just descendant element nodes, and so it removed this text node as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文