【发布时间】:2019-03-27 09:26:05
【问题描述】:
我有C# 申请。下面是我的 XML
<subscription>
<subscription_add_ons type="array">
<subscription_add_on>
<add_on_code>bike-o-vision</add_on_code>
<quantity type="integer">1</quantity>
</subscription_add_on>
<subscription_add_on>
<add_on_code>boxx</add_on_code>
<quantity type="integer">1</quantity>
</subscription_add_on>
</subscription_add_ons>
</subscription>
我需要的是,如果我通过字符串 addOnCode = boxx,删除完整的节点,即,
<subscription_add_on>
<add_on_code>boxx</add_on_code>
<quantity type="integer">1</quantity>
</subscription_add_on>
功能
XDocument xmlDoc = XDocument.Parse(xmlString);
XElement element = new XElement(
"subscription_add_on",
new XElement("add_on_code", "box"),
new XElement("quantity",
new XAttribute("type", "integer"),
1
)
);
xmlDoc.Root.Descendants(element.Name).Remove();
但不知何故,它并没有按需要删除。
如何使用 XDocument 做到这一点?
谢谢!
【问题讨论】:
-
您的元素与您的文档无关。这只是一个“松散”的元素。您需要在文档中找到该元素,然后将其删除。
-
@PalleDue 同意,但如何
标签: c# linq-to-xml xmldocument c#-7.0