删除XML节点集合中的空/空白元素

我有一个像这样的XML文档:

  400 Attribute weight is not applicable for product type Configurable Product   400 Resource data pre-validation error.   1     No code was given   

我正在尝试迭代每个节点并执行以下操作:

  1. 扔掉任何空/空白的元素。
  2. 仅使用包含值的元素生成新节点。
  3. 将生成的文档发送到不同的Web服务。

我正在努力的部分是如何遍历每个节点并检查每个元素的空值。

我一直在http://rextester.com/runco​​de上测试这段代码,但似乎无法弄清楚:

 Console.WriteLine("Querying tree loaded with XElement.Load"); Console.WriteLine("----"); XElement doc = XElement.Parse(@"  400 Attribute weight is not applicable for product type Configurable Product   400 Resource data pre-validation error.   1     No code was given  "); int counter = 1; IEnumerable nodes = from nd in doc.Nodes() select nd; foreach (XNode node in nodes) { Console.WriteLine(counter + "-" + node); IEnumerable elements = from el in node //this is where I've been trying various methods, but no dice. select el; foreach (XElement e in elements) { Console.WriteLine(counter + "-" + e.Name + "-" + e.Value + "\r\n"); } counter++; } 

基于上面的XML输入,我希望得到以下输出:

   400 Attribute weight is not applicable for product type Configurable Product   400 Resource data pre-validation error.   1   No code was given   

我不确定我是否使用正确的方法迭代节点和元素。

单个单行可以完成工作,无需迭代所有元素。 它来了:

 doc.Descendants().Where(e => string.IsNullOrEmpty(e.Value)).Remove(); 

测试仪

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.Xml.Linq; namespace ConsoleApplication1 { public class TestRemove { public static void Main() { Console.WriteLine("----OLD TREE STARTS---"); XElement doc = XElement.Parse(@"  400 Attribute weight is not applicable for product type Configurable Product   400 Resource data pre-validation error.   1     No code was given  "); Console.Write(doc.ToString()); Console.WriteLine(""); Console.WriteLine("----OLD TREE ENDS---"); Console.WriteLine(""); doc.Descendants().Where(e => string.IsNullOrEmpty(e.Value)).Remove(); Console.WriteLine("----NEW TREE STARTS---"); Console.Write(doc.ToString()); Console.WriteLine(""); Console.WriteLine("----NEW TREE ENDS---"); Console.ReadKey(); } } } 

它也可以在这里测试

 doc.Descendants().Where(e => string.IsNullOrEmpty(e.Value)).Remove(); 

这一行不会抛出充满空子标记的空父标记。 它只会移除他们的孩子,这可能适合您的情况,也可能不适合您的情况。 实现这一目标是一个非常简单的变化,您只需要首先从最低级别开始删除。 就像是

 foreach(XElement child in doc.Descendants().Reverse()) { if(!child.HasElements && string.IsNullOrEmpty(child.Value) && !child.HasAttributes) child.Remove(); } 

感谢Nyerguds的属性建议。

在VB中我需要再次找到它:

 doc.Descendants().Where(Function(e) String.IsNullOrEmpty(e.Value)).Remove()