我怎样才能通过知道其中的一个元素来删除整个 XML 块
How can i remove an whole XML Block just by knowing an element inside it
大家有一个问题,关于如何从我的 xml 文件中删除一个元素块,同时只知道其中一个项目的文本值。有数百个相同的块,唯一的区别是 ID 值和我知道删除块的文本。
public static void deleteBlock() {
XElement xelement = XElement.Load(@"C:\Program Files (x86)\Tools\VisualStudioProjects\bausteine\modul.xml");
foreach (XElement xEle in xelement.Descendants("SW.Blocks.CompileUnit"))
{
var complete = xEle;
foreach(var item in xEle.Descendants("Text"))
{
if (item.Value=="The only thing i know to delete the Block")
{
complete.Remove();
break; // The Answer
}
}
xelement.Save(@"C:\Program Files(x86)\Tools\VisualStudioProjects\Baustein_1.xml");
}
虽然我可以先查找名称为 SW.Blocks.CompileUnit 的元素,然后在其中查找文本值,如果它与我的相匹配,那么它应该删除该块并且它应该遍历所有块。它找到了我想要的块,但删除了所有其他内容并保存了我想要删除的块。然后它给了我 Null Exception。
<SW.Blocks.CompileUnit ID="85" CompositionName="CompileUnits">
<AttributeList>
<ObjectList>
<MultilingualText ID="86" CompositionName="Comment">
<ObjectList>
<MultilingualTextItem ID="87" CompositionName="Items">
<AttributeList>
<Culture>de-DE</Culture>
<Text />
</AttributeList>
</MultilingualTextItem>
</ObjectList>
</MultilingualText>
<MultilingualText ID="88" CompositionName="Title">
<ObjectList>
<MultilingualTextItem ID="89" CompositionName="Items">
<AttributeList>
<Culture>de-DE</Culture>
<Text>The Only thing i know to delete the Block</Text>
</AttributeList>
</MultilingualTextItem>
</ObjectList>
</MultilingualText>
</ObjectList>
</SW.Blocks.CompileUnit>
那是 xml 数据的块之一。这也是我的代码在删除其他所有内容时留下的代码。我希望我现在解释得更好一点。我不知道为什么它会删除除块之外的所有其他内容,为什么它会给我一个空异常。提前致谢
在这种情况下,我建议在确定需要删除哪些元素后使用 Remove
extension method。这是一个完整的程序来演示这一点 - 它只需要一个合适的 input.xml
文件。
using System.Linq;
using System.Xml.Linq;
var element = XElement.Load("input.xml");
element
.Descendants("SW.Blocks.CompileUnit")
.Where(x => x.Descendants("Text")
.Any(x => x.Value == "The only thing i know to delete the Block"))
.Remove();
element.Save("result.xml");
这不仅高效,而且 self-descriptive 比一次一个地遍历后代的手动代码要好。
大家有一个问题,关于如何从我的 xml 文件中删除一个元素块,同时只知道其中一个项目的文本值。有数百个相同的块,唯一的区别是 ID 值和我知道删除块的文本。
public static void deleteBlock() {
XElement xelement = XElement.Load(@"C:\Program Files (x86)\Tools\VisualStudioProjects\bausteine\modul.xml");
foreach (XElement xEle in xelement.Descendants("SW.Blocks.CompileUnit"))
{
var complete = xEle;
foreach(var item in xEle.Descendants("Text"))
{
if (item.Value=="The only thing i know to delete the Block")
{
complete.Remove();
break; // The Answer
}
}
xelement.Save(@"C:\Program Files(x86)\Tools\VisualStudioProjects\Baustein_1.xml");
}
虽然我可以先查找名称为 SW.Blocks.CompileUnit 的元素,然后在其中查找文本值,如果它与我的相匹配,那么它应该删除该块并且它应该遍历所有块。它找到了我想要的块,但删除了所有其他内容并保存了我想要删除的块。然后它给了我 Null Exception。
<SW.Blocks.CompileUnit ID="85" CompositionName="CompileUnits">
<AttributeList>
<ObjectList>
<MultilingualText ID="86" CompositionName="Comment">
<ObjectList>
<MultilingualTextItem ID="87" CompositionName="Items">
<AttributeList>
<Culture>de-DE</Culture>
<Text />
</AttributeList>
</MultilingualTextItem>
</ObjectList>
</MultilingualText>
<MultilingualText ID="88" CompositionName="Title">
<ObjectList>
<MultilingualTextItem ID="89" CompositionName="Items">
<AttributeList>
<Culture>de-DE</Culture>
<Text>The Only thing i know to delete the Block</Text>
</AttributeList>
</MultilingualTextItem>
</ObjectList>
</MultilingualText>
</ObjectList>
</SW.Blocks.CompileUnit>
那是 xml 数据的块之一。这也是我的代码在删除其他所有内容时留下的代码。我希望我现在解释得更好一点。我不知道为什么它会删除除块之外的所有其他内容,为什么它会给我一个空异常。提前致谢
在这种情况下,我建议在确定需要删除哪些元素后使用 Remove
extension method。这是一个完整的程序来演示这一点 - 它只需要一个合适的 input.xml
文件。
using System.Linq;
using System.Xml.Linq;
var element = XElement.Load("input.xml");
element
.Descendants("SW.Blocks.CompileUnit")
.Where(x => x.Descendants("Text")
.Any(x => x.Value == "The only thing i know to delete the Block"))
.Remove();
element.Save("result.xml");
这不仅高效,而且 self-descriptive 比一次一个地遍历后代的手动代码要好。