views:

223

answers:

1

Hi, my problem is the following: How can I remove selected ChildNodes from XmlNode recursively? My XML-file looks like...

..<element type="TextBox" id="xslFilePath">
                <parameters>
                    <parameter id="description">
                        <value><![CDATA[Pfad zur XSL]]></value>
                        <value lang="en"><![CDATA[XSL-file's path]]></value>
                    </parameter>
                    <parameter id="tooltip">
                        <value><![CDATA[Pfad zur XSL]]></value>
                        <value lang="en"><![CDATA[XSL-file's path]]></value>
                    </parameter>
                </parameters>
                <values>
                    <value><![CDATA[/include/extensions/languageReferences/xsl/default.xsl]]></value>
                </values>
            </element>
            <element type="DropDownList" id="imageOrientation">
                <parameters>
                    <parameter id="description">
                        <value><![CDATA[Anordnung]]></value>
                        <value lang="en"><![CDATA[Orientation]]></value>
                    </parameter>
                    <parameter id="tooltip">
                        <value><![CDATA[Anordnung]]></value>
                        <value lang="en"><![CDATA[Orientation]]></value>
                    </parameter>
                </parameters>
                <items>
                    <item id="" selected="true">
                        <parameters>
                            <parameter id="value">
                                <value><![CDATA[vertical]]></value>
                            </parameter>
                            <parameter id="description">
                                <value><![CDATA[senkrecht]]></value>
                                <value lang="en"><![CDATA[vertical]]></value>
                            </parameter>
                        </parameters>
                    </item>
                    <item id="" selected="false">
                        <parameters>
                            <parameter id="value">
                                <value><![CDATA[horizontal]]></value>
                            </parameter>
                            <parameter id="description">
                                <value><![CDATA[waagerecht]]></value>
                                <value lang="en"><![CDATA[horizontal]]></value>
                            </parameter>
                        </parameters>
                    </item>
                </items>
                <values>
                    <value><![CDATA[horizontal]]></value>
                </values>
            </element>...

I would like to remove all nodes (type of value) where the parentNode is type of parameter with id="description" but not value-notes as children of values or parameter with id="value" In XSLT I would say e.g.: //value[parent::parameter[@id='description'] and @lang='en']The problem is: I have the language code: e.g. "de" and now I would like to remove all sibling value nodes if an value with lang="de" exists and remove all sibling nodes excluding the value without any lang-attribute if lang="de" not exists (as fallback) I hope, you can help me to write an c# Code to replace recursively all undesired value-nodes.

+1  A: 

Hopefully this is what you´r looking for.

If you load you´r xml into a XmlDocument you can use a method like this one to remove nodes matching an xpath.

public void RemoveElements(XmlDocument document, string xpathForElementsToRemove)
{
    if (document == null || document.DocumentElement == null) return;
    var xmlNodeList = document.DocumentElement.SelectNodes(xpathForElementsToRemove);
    if (xmlNodeList == null || xmlNodeList.Count == 0) return;
    for (var n = xmlNodeList.Count - 1; n >= 0; n--)
    {
        var xmlNode = xmlNodeList[n];
        if (xmlNode.ParentNode == null) continue;
        xmlNode.ParentNode.RemoveChild(xmlNode);
    }
}
Jens Granlund