如何从xml中删除列表父节点?

Posted

技术标签:

【中文标题】如何从xml中删除列表父节点?【英文标题】:How to remove List parent node from xml? 【发布时间】:2021-06-14 21:09:15 【问题描述】:

我已尝试使用以下代码从以下 XML 中删除 scheme-details1,但无法将其删除。

XmlActionResult xmlActionResult = new XmlActionResult(schememaster);
foreach (XElement element in xmlActionResult._document.Elements()) 
 
    xmlActionResult._document.Elements("scheme-details1").Remove(); 

其中xmlActionResult._documentXDocument

以下是我的 XML 响应:-

<scheme-master>
    <general-information>
        <schemecode>XXXXX</schemecode>
        <year>2020</year>
        <month>04</month>
        <requestid>0008052020</requestid>
    </general-information>
    <scheme-details1>
        <scheme-details>
            <location>
                <district_code>503</district_code>
                <district_name>Chittoor</district_name>
                <state_code>28</state_code>
                <state_name>Andhra pradesh</state_name>
            </location>
            <beneficiary-details>
                <no_of_beneficiaries_normative_central_and_state_share>10000</no_of_beneficiaries_normative_central_and_state_share>
                <no_of_additional_beneficiaries_supported_by_state>0</no_of_additional_beneficiaries_supported_by_state>
                <total_no_of_beneficiaries>10000</total_no_of_beneficiaries>
                <no_of_beneficiaries_record_digitized>10000</no_of_beneficiaries_record_digitized>
                <no_of_authenticated_seeded_beneficiaries>10000</no_of_authenticated_seeded_beneficiaries>
                <no_of_beneficiaries_whom_mobile_no_captured>10000</no_of_beneficiaries_whom_mobile_no_captured>
            </beneficiary-details>
            <fundtransfer-details>
                <central_share_fund_transferred_cash>10000</central_share_fund_transferred_cash>
                <normative_state_share_fund_transferred_cash>0</normative_state_share_fund_transferred_cash>
                <additional_state_share_fund_transferred_cash>0</additional_state_share_fund_transferred_cash>
                <state_share_fund_transferred_to_additional_beneficiaries_cash>0</state_share_fund_transferred_to_additional_beneficiaries_cash>
                <total_fund_transferred_cash>10000</total_fund_transferred_cash>
                <central_share_expenditure_incurred_inkind>0</central_share_expenditure_incurred_inkind>
                <normative_state_share_expenditure_incurred_inkind>0</normative_state_share_expenditure_incurred_inkind>
                <additional_state_share_expenditure_incurred_inkind>0</additional_state_share_expenditure_incurred_inkind>
                <state_share_expenditure_incurred_to_additional_beneficiaries_inkind>0</state_share_expenditure_incurred_to_additional_beneficiaries_inkind>
                <total_expenditure_incurred_inkind>0</total_expenditure_incurred_inkind>
            </fundtransfer-details>
            <transaction-details>
                <total_no_transactions_electronic_modes_cash>10000</total_no_transactions_electronic_modes_cash>
                <payment_electronic_modes_cash>10000</payment_electronic_modes_cash>
                <total_no_transactions_other_modes_cash>0</total_no_transactions_other_modes_cash>
                <payment_other_modes_cash>0</payment_other_modes_cash>
                <quantity_transferred_inkind>0</quantity_transferred_inkind>
                <no_of_authenticated_transactions_inkind>0</no_of_authenticated_transactions_inkind>
                <dbt_expenditure_incurred_inkind>0</dbt_expenditure_incurred_inkind>
            </transaction-details>
        </scheme-details>
        <scheme-details>
            <location>
                <district_code>504</district_code>
                <district_name>Cuddapah (YSR Kadapa)</district_name>
                <state_code>28</state_code>
                <state_name>Andhra pradesh</state_name>
            </location>
            <beneficiary-details>
                <no_of_beneficiaries_normative_central_and_state_share>10000</no_of_beneficiaries_normative_central_and_state_share>
                <no_of_additional_beneficiaries_supported_by_state>0</no_of_additional_beneficiaries_supported_by_state>
                <total_no_of_beneficiaries>10000</total_no_of_beneficiaries>
                <no_of_beneficiaries_record_digitized>10000</no_of_beneficiaries_record_digitized>
                <no_of_authenticated_seeded_beneficiaries>10000</no_of_authenticated_seeded_beneficiaries>
                <no_of_beneficiaries_whom_mobile_no_captured>10000</no_of_beneficiaries_whom_mobile_no_captured>
            </beneficiary-details>
            <fundtransfer-details>
                <central_share_fund_transferred_cash>10000</central_share_fund_transferred_cash>
                <normative_state_share_fund_transferred_cash>0</normative_state_share_fund_transferred_cash>
                <additional_state_share_fund_transferred_cash>0</additional_state_share_fund_transferred_cash>
                <state_share_fund_transferred_to_additional_beneficiaries_cash>0</state_share_fund_transferred_to_additional_beneficiaries_cash>
                <total_fund_transferred_cash>10000</total_fund_transferred_cash>
                <central_share_expenditure_incurred_inkind>0</central_share_expenditure_incurred_inkind>
                <normative_state_share_expenditure_incurred_inkind>0</normative_state_share_expenditure_incurred_inkind>
                <additional_state_share_expenditure_incurred_inkind>0</additional_state_share_expenditure_incurred_inkind>
                <state_share_expenditure_incurred_to_additional_beneficiaries_inkind>0</state_share_expenditure_incurred_to_additional_beneficiaries_inkind>
                <total_expenditure_incurred_inkind>0</total_expenditure_incurred_inkind>
            </fundtransfer-details>
            <transaction-details>
                <total_no_transactions_electronic_modes_cash>10000</total_no_transactions_electronic_modes_cash>
                <payment_electronic_modes_cash>10000</payment_electronic_modes_cash>
                <total_no_transactions_other_modes_cash>0</total_no_transactions_other_modes_cash>
                <payment_other_modes_cash>0</payment_other_modes_cash>
                <quantity_transferred_inkind>0</quantity_transferred_inkind>
                <no_of_authenticated_transactions_inkind>0</no_of_authenticated_transactions_inkind>
                <dbt_expenditure_incurred_inkind>0</dbt_expenditure_incurred_inkind>
            </transaction-details>
        </scheme-details>
        <scheme-details>
            <location>
                <district_code>503</district_code>
                <district_name>Chittoor</district_name>
                <state_code>28</state_code>
                <state_name>Andhra pradesh</state_name>
            </location>
            <beneficiary-details>
                <no_of_beneficiaries_normative_central_and_state_share>10000</no_of_beneficiaries_normative_central_and_state_share>
                <no_of_additional_beneficiaries_supported_by_state>0</no_of_additional_beneficiaries_supported_by_state>
                <total_no_of_beneficiaries>10000</total_no_of_beneficiaries>
                <no_of_beneficiaries_record_digitized>10000</no_of_beneficiaries_record_digitized>
                <no_of_authenticated_seeded_beneficiaries>10000</no_of_authenticated_seeded_beneficiaries>
                <no_of_beneficiaries_whom_mobile_no_captured>10000</no_of_beneficiaries_whom_mobile_no_captured>
            </beneficiary-details>
            <fundtransfer-details>
                <central_share_fund_transferred_cash>10000</central_share_fund_transferred_cash>
                <normative_state_share_fund_transferred_cash>0</normative_state_share_fund_transferred_cash>
                <additional_state_share_fund_transferred_cash>0</additional_state_share_fund_transferred_cash>
                <state_share_fund_transferred_to_additional_beneficiaries_cash>0</state_share_fund_transferred_to_additional_beneficiaries_cash>
                <total_fund_transferred_cash>10000</total_fund_transferred_cash>
                <central_share_expenditure_incurred_inkind>0</central_share_expenditure_incurred_inkind>
                <normative_state_share_expenditure_incurred_inkind>0</normative_state_share_expenditure_incurred_inkind>
                <additional_state_share_expenditure_incurred_inkind>0</additional_state_share_expenditure_incurred_inkind>
                <state_share_expenditure_incurred_to_additional_beneficiaries_inkind>0</state_share_expenditure_incurred_to_additional_beneficiaries_inkind>
                <total_expenditure_incurred_inkind>0</total_expenditure_incurred_inkind>
            </fundtransfer-details>
            <transaction-details>
                <total_no_transactions_electronic_modes_cash>10000</total_no_transactions_electronic_modes_cash>
                <payment_electronic_modes_cash>10000</payment_electronic_modes_cash>
                <total_no_transactions_other_modes_cash>0</total_no_transactions_other_modes_cash>
                <payment_other_modes_cash>0</payment_other_modes_cash>
                <quantity_transferred_inkind>0</quantity_transferred_inkind>
                <no_of_authenticated_transactions_inkind>0</no_of_authenticated_transactions_inkind>
                <dbt_expenditure_incurred_inkind>0</dbt_expenditure_incurred_inkind>
            </transaction-details>
        </scheme-details>
        <scheme-details>
            <location>
                <district_code>504</district_code>
                <district_name>Cuddapah (YSR Kadapa)</district_name>
                <state_code>28</state_code>
                <state_name>Andhra pradesh</state_name>
            </location>
            <beneficiary-details>
                <no_of_beneficiaries_normative_central_and_state_share>10000</no_of_beneficiaries_normative_central_and_state_share>
                <no_of_additional_beneficiaries_supported_by_state>0</no_of_additional_beneficiaries_supported_by_state>
                <total_no_of_beneficiaries>10000</total_no_of_beneficiaries>
                <no_of_beneficiaries_record_digitized>10000</no_of_beneficiaries_record_digitized>
                <no_of_authenticated_seeded_beneficiaries>10000</no_of_authenticated_seeded_beneficiaries>
                <no_of_beneficiaries_whom_mobile_no_captured>10000</no_of_beneficiaries_whom_mobile_no_captured>
            </beneficiary-details>
            <fundtransfer-details>
                <central_share_fund_transferred_cash>10000</central_share_fund_transferred_cash>
                <normative_state_share_fund_transferred_cash>0</normative_state_share_fund_transferred_cash>
                <additional_state_share_fund_transferred_cash>0</additional_state_share_fund_transferred_cash>
                <state_share_fund_transferred_to_additional_beneficiaries_cash>0</state_share_fund_transferred_to_additional_beneficiaries_cash>
                <total_fund_transferred_cash>10000</total_fund_transferred_cash>
                <central_share_expenditure_incurred_inkind>0</central_share_expenditure_incurred_inkind>
                <normative_state_share_expenditure_incurred_inkind>0</normative_state_share_expenditure_incurred_inkind>
                <additional_state_share_expenditure_incurred_inkind>0</additional_state_share_expenditure_incurred_inkind>
                <state_share_expenditure_incurred_to_additional_beneficiaries_inkind>0</state_share_expenditure_incurred_to_additional_beneficiaries_inkind>
                <total_expenditure_incurred_inkind>0</total_expenditure_incurred_inkind>
            </fundtransfer-details>
            <transaction-details>
                <total_no_transactions_electronic_modes_cash>10000</total_no_transactions_electronic_modes_cash>
                <payment_electronic_modes_cash>10000</payment_electronic_modes_cash>
                <total_no_transactions_other_modes_cash>0</total_no_transactions_other_modes_cash>
                <payment_other_modes_cash>0</payment_other_modes_cash>
                <quantity_transferred_inkind>0</quantity_transferred_inkind>
                <no_of_authenticated_transactions_inkind>0</no_of_authenticated_transactions_inkind>
                <dbt_expenditure_incurred_inkind>0</dbt_expenditure_incurred_inkind>
            </transaction-details>
        </scheme-details>
    </scheme-details1>
</scheme-master>

【问题讨论】:

请更新您的问题,并添加所需的输出 XML。 在循环中你需要做element.Elements("scheme-details1").Remove();。或者消除循环并执行xmlActionResult._document.Root.Elements("scheme-details1").Remove();。见dotnetfiddle.net/0vZMoK 当你有一个列表时,你必须从末尾开始并朝着开头移动: List elements = xmlActionResult._document.Elements().ToList(); for (int i = elements.Count - 1; i >= 0; i--) elements[i].Remove; 在您的问题中,您定义了一个变量xmlActionResult1,但随后尝试从xmlActionResult 的文档中删除节点。假设这是问题中的错字,我编辑了变量名称以保持一致。如果您的代码中确实有两个不同的XmlActionResult 对象,这也可以解释您的问题——您可能正在从错误的对象中删除节点。 【参考方案1】:

您正在循环通过 XDocumentXElement element 根节点,然后尝试从文档本身中删除 &lt;scheme-details1&gt; 节点。相反,将它们从当前的element 中删除:

foreach (var element in xmlActionResult._document.Elements()) 
 
    //xmlActionResult._document.Elements("scheme-details1").Remove(); // FIXED
    element.Elements("scheme-details1").Remove();               
       

或者,由于格式良好的 XML 文档总是只有一个 root element,您可以消除根元素的循环,然后这样做:

xmlActionResult._document.Root.Elements("scheme-details1").Remove(); 

演示here.

更新

Remove() 从父节点中删除节点集合及其所有后代。如果您想删除&lt;scheme-details1&gt; 节点并将其所有子节点冒泡到其父节点,您可以创建以下扩展方法:

public static class XNodeExtensions

    public static void RemoveAndMoveChildrenUp (this IEnumerable<XNode> source)
    
        foreach (var node in source.ToList())
            node.RemoveAndMoveChildrenOfTypeUp<XNode>();
    
    
    public static void RemoveAndMoveChildElementsUp (this IEnumerable<XNode> source)
    
        foreach (var node in source.ToList())
            node.RemoveAndMoveChildrenOfTypeUp<XElement>();
    
    
    public static void RemoveAndMoveChildrenOfTypeUp<TChild>(this XNode node) where TChild : XNode
    
        var parent = node.Parent;
        if (node is XContainer container)
        
            for (XNode child = container.FirstNode, nextChild = child?.NextNode; child != null; child = nextChild, nextChild = child?.NextNode)
            
                if (child is TChild)
                
                    child.Remove(); // Remove the child first to prevent cloning when added to the parent
                    parent.Add(child);
                
            
        
        node.Remove();
    

然后做:

xmlActionResult._document.Root.Elements("scheme-details1").RemoveAndMoveChildrenUp(); 

或者,如果&lt;scheme-details1&gt; 是mixed content 节点并且您只想冒泡子元素,请使用RemoveAndMoveChildElementsUp()

演示小提琴#2 here.

【讨论】:

也尝试过同样的方法,它会返回 Void。我通过这样做得到了一个解决方案,我只是用空替换了那个标签。字符串 xml = doc.ToString(); xml = xml.Replace("", ""); xml = xml.Replace("", ""); doc = XDocument.Parse(xml); 在你上面的回答中,删除 scheme-details1 是正确的,但是它完全删除了整个,我只想从 XML 中删除那个 TAG 而不是整个 Scheme-Details1 块。【参考方案2】:

上面发布的答案也有效,并且通过替换 XML 中的标签,得到的答案也适用于我。

            XDocument doc = new XDocument();
            using (var writer = doc.CreateWriter())
            
                // write xml into the writer
                var serializer = new DataContractSerializer(obj.GetType());
                serializer.WriteObject(writer, obj);
            
            string xml = doc.ToString();
            xml = xml.Replace("<scheme-details1>", "");
            xml = xml.Replace("</scheme-details1>", "");
            doc = XDocument.Parse(xml);

【讨论】:

以上是关于如何从xml中删除列表父节点?的主要内容,如果未能解决你的问题,请参考以下文章

使用 Linq 删除 XML 中的子节点

删除没有子节点的父节点

从xmlDoc中删除节点,如何删除父节点?

如何根据子节点删除父节点和/或 css 样式

完成播放后,我是不是必须手动从父节点中删除发射器节点?

如何使用 C# 从 XML 节点列表中删除节点