English 中文(简体)
利用LINQ从XML档案中删除所有节点的问题
原标题:Issue in removing all the nodes from XML file using LINQ

我正试图从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>

预期的OP:

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

实际操作:EDITED

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

CODE:

下面是正在使用的法典,其概念是错误算法,而时间档则被登录在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()。

<root /> is valid XML for a tag with no content ( Self-closing tags). 如果你绝对需要一个开放和封闭的标签,那么你就必须把某些内容放在像评论或案文这样的根本的 no中。

在你的第一句话中,混淆不清:“我正试图从XML档案中删除所有节点/点。 是什么? 您是否想删除所有节点或所有内容?

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.

您的话,<root/> 是删除所有后代子的代码正确输出:它有一个名为root且无内容的单一要素。

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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签