- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
这个有点棘手。假设我有这个 XmlDocument
<Object>
<Property1>1</Property1>
<Property2>2</Property2>
<SubObject>
<DeeplyNestedObject />
</SubObject>
</Object>
我想找回这个
<Object>
<Property1>1</Property1>
<Property2>2</Property2>
</Object>
由于 <SubObject>
的每个 child 都是我想摆脱的空元素。具有挑战性的是您无法在遍历节点时删除节点。任何帮助将不胜感激。
更新这是我最后的结果。
public XDocument Process()
{
//Load my XDocument
var xmlDoc = GetObjectXml(_source);
//Keep track of empty elements
var childrenToDelete = new List<XElement>();
//Recursively iterate through each child node
foreach (var node in xmlDoc.Root.Elements())
Process(node, childrenToDelete);
//An items marked for deletion can safely be removed here
//Since we're not iterating over the source elements collection
foreach (var deletion in childrenToDelete)
deletion.Remove();
return xmlDoc;
}
private void Process(XElement node, List<XElement> elementsToDelete)
{
//Walk the child elements
if (node.HasElements)
{
//This is the collection of child elements to be deleted
//for this particular node
var childrenToDelete = new List<XElement>();
//Recursively iterate each child
foreach (var child in node.Elements())
Process(child, childrenToDelete);
//Delete all children that were marked as empty
foreach (var deletion in childrenToDelete)
deletion.Remove();
//Since we just removed all this nodes empty children
//delete it if there's nothing left
if (node.IsEmpty)
elementsToDelete.Add(node);
}
//The current leaf node is empty so mark it for deletion
else if (node.IsEmpty)
elementsToDelete.Add(node);
}
如果有人对此的用例感兴趣,那就是 ObjectFilter我放在一起的项目。
最佳答案
它会很慢,但你可以这样做:
XElement xml;
while (true) {
var empties = xml.Descendants().Where(x => x.IsEmpty && !x.HasAttributes).ToList();
if (empties.Count == 0)
break;
empties.ForEach(e => e.Remove());
}
为了加快速度,您可以在第一次迭代后向上遍历父节点,看看它们是否为空。
XElement xml;
var empties = xml.Descendants().Where(x => x.IsEmpty && !x.HasAttributes).ToList();
while (empties.Count > 0) {
var parents = empties.Select(e => e.Parent)
.Where(e => e != null)
.Distinct() //In case we have two empty siblings, don't try to remove the parent twice
.ToList();
empties.ForEach(e => e.Remove());
//Filter the parent nodes to the ones that just became empty.
parents.RemoveAll(e => e.IsEmpty && !e.HasAttributes);
empties = parents;
}
关于c# - 如何删除所有空的 XElements,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11052420/
我第一次在 .NET 3.5 中使用 Linq for XML,但我在命名空间方面遇到了一些问题。即,XElement 打印如下:当我只想说 . 这是代码: XNamespace opfNa
请考虑这个 XML: 1000 Nima Agha 1002 Ligha
我的查询返回类型是IEnumerable .如何将结果数据转换为 XElement类型?可能吗?有人可以帮助我理解这一点。 var resQ = from e in docElmnt.Descenda
有没有办法用其他 XElement 有选择地替换 XElement 内容? 我有这个 XML: There is something I want to tell you.[pause=3]
我正在尝试使用 Linq 对 XElement 的子项进行排序,然后将现有的子项替换为已排序。 首先我创建 XElement: XElement WithLinq = new
在尝试添加某个元素之前,如何检查该元素是否存在于给定元素中? 背景:我有一个 XDocument X 作为子元素包含 Flowers 随后包含一个系列每个元素都命名为 Flower。每个 Flower
我有一个为列表对象生成 xml 的函数: public XDocument ToXML() { foreach (var row in this) { var xml
我有一个如下所示的 XML 文档: ..... ..... 我想选择所有 lev
我有一个名为“XUsers”的 XElement,它将包含如下所示的 XML: ...还有另一个 XElement,称为“XTasks”,它将包含如下数据:
我希望创建一个具有多个 XElement 子节点的顶级 XElement,如下所示。 Mike Smith Sam
假设我有以下 xml, ... 现在我想“插入”一个围绕 BeginsWith 子句的子句,所以它看起来像这样, ... 我如何使用 LinqToXml 实现这一点? 我主要做的 Add 方法 whe
我想从 B 中选择 COMMISSION 元素,其中每个 COMMISSION 元素的 ID 值不等于 A COMMISSION 元素。 这是我完整的 Main 方法: static void
我正在解析一个大的 xml 文件。所以我将 XmlReader 与 XElement 结合使用,而不是 XElement.Load()。 我已经从 XmlReader 创建了 XElement 对象,
我正在使用 C# .Net 3.5 并尝试将给定的 xml (XDocument) 转换为一个空的(其中 XElement.IsEmpty 为真)不包含文本值。我尝试将 XElement.Value
这是我的示例 xml: Title of RSS feed LINK Details about the feed en TITLE
我有一段类似下面的 xml: Value1_1 Value1_2 Value2_1 Value2_2 我想要的是生成一个 IEnumerab
给定以下 XML: ...以及对“SomeElement”的 XElement 引用和 XPath“Element1/Element2/@Attribute3”
我无法在迭代节点时删除节点,没关系。´ 我有一个包含 Guid 的列表。 我想删除该 xml 文件中的所有 XElement,其中 XElement 具有该列表的 Guid 那是我的 xml 文件:
我收到以下错误: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable>' to 'System.Collect
我收到以下错误: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable>' to 'System.Collect
我是一名优秀的程序员,十分优秀!