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

How to remove List parent node from 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>

您正在遍历 XDocumentXElement element 根节点,然后尝试从文档本身中删除 <scheme-details1> 节点。相反,将它们从当前 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() 从它们的父节点中删除节点集合 及其所有后代 。如果要删除 <scheme-details1> 节点并将其所有子节点冒泡到其父节点,可以创建以下扩展方法:

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(); 

或者,如果 <scheme-details1> 是一个 mixed content 节点,而您只想冒泡子 元素 ,请使用 RemoveAndMoveChildElementsUp()

演示 fiddle #2 here.

上面发布的答案也有效,并且通过替换 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);