使用 LINQ 从 XML 文件中删除所有节点时出现问题
我正在尝试从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 docs.Root.Nodes().Remove()。
Use
docs.Root.Nodes().Remove()
.
是没有内容的标签(自闭合标签)的有效 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.混乱在于您的第一句话:“我正在尝试从 XML 文件中删除所有节点/元素。”是哪一个?您想删除所有节点还是所有元素?
XML中有五种类型的节点:元素、文本、注释、处理指令和属性。如果您交替使用“节点”和“元素”,就像您在这里一样,那么您在使用 XML 时将会遇到无穷无尽的麻烦。
您得到的
是删除所有后代节点的代码的正确输出:它是一个名为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 namedroot
with no content.What you expect,
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.