提问者:小点点

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


我尝试使用下面的代码从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>

共1个答案

匿名用户

您正在循环访问XDocumentXElement Element根节点,但尝试从文档本身删除节点。而是从当前的元素中删除它们:

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

或者,由于格式良好的XML文档总是只有一个根元素,您可以省去根元素的循环,只需执行以下操作:

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

这里是演示。