locked
Removing Duplicate Element Entries from the XML file using XLinq RRS feed

  • Question

  • User-823648204 posted

    Hi ,

    I want to modify duplicate xml elment entries to single element  in xml file. As of now i am having "attribute" as element for removing ...In future i will need it for any element. Below is the current xml format:

    <?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
    <node_1_list:nodes xmlns:node_1_list="xyz/schemas/node_1_list">

    <node_1:node name="r2l3e4vm4" hostname="r2l3e4vm4" guid="000000505688128b0000000300000001" xmlns:node_1_list="xyz/schemas/node_1_list" xmlns:node_1="xyz/schemas/node_1" xmlns:attribute_1="xyz/schemas/attribute_1">
    <attribute_1:attribute name="DeviceType">Server</attribute_1:attribute>
    <attribute_1:attribute name="DeviceSubType">VMGuest</attribute_1:attribute>
    <attribute_1:attribute name="DeviceSubType">x86System</attribute_1:attribute>

    <attribute_1:attribute name="CIM_NetworkAdapter.creationClassName">CIM_NetworkAdapter</attribute_1:attribute>
        <attribute_1:attribute name="CIM_NetworkAdapter.creationClassName">CIM_NetworkAdapter</attribute_1:attribute>
        <attribute_1:attribute name="CIM_NetworkAdapter.creationClassName">CIM_NetworkAdapter</attribute_1:attribute>
        <attribute_1:attribute name="CIM_NetworkAdapter.creationClassName">CIM_NetworkAdapter</attribute_1:attribute>
        <attribute_1:attribute name="CIM_NetworkAdapter.creationClassName">CIM_NetworkAdapter</attribute_1:attribute>
        <attribute_1:attribute name="CIM_NetworkAdapter.creationClassName">CIM_NetworkAdapter</attribute_1:attribute>
        <attribute_1:attribute name="CIM_NetworkAdapter.creationClassName">CIM_NetworkAdapter</attribute_1:attribute>
        <attribute_1:attribute name="CIM_NetworkAdapter.creationClassName">CIM_NetworkAdapter</attribute_1:attribute>
        <attribute_1:attribute name="CIM_NetworkAdapter.creationClassName">CIM_NetworkAdapter</attribute_1:attribute>
        <attribute_1:attribute name="CIM_NetworkAdapter.creationClassName">CIM_NetworkAdapter</attribute_1:attribute>
    </node_1:node>

    </node_1_list:nodes>

     

    Please help for above attribute as well as for general purpose.Its urgent.

    Monday, May 24, 2010 3:46 PM

Answers

  • User1835330922 posted

    There is a method XNode.DeepEquals that you can use as follows (for the general case):

            static void Main()
            {
                XDocument input = XDocument.Load("input.xml");
                XDocument output = new XDocument(
                    FilterDuplicates(input.Nodes())
                    );
    
                output.Save("output.xml");
            }
    
            private static IEnumerable<XNode> FilterDuplicates(IEnumerable<XNode> nodes)
            {
                foreach (XNode node in nodes.Where(n => !n.NodesBeforeSelf().Any(s => XNode.DeepEquals(n, s))))
                {
                    switch (node.NodeType)
                    {
                        case XmlNodeType.Element:
                            XElement elNode = node as XElement;
                            yield return new XElement(elNode.Name, elNode.Attributes(), FilterDuplicates(elNode.Nodes()));
                            break;
                        case XmlNodeType.Text:
                            yield return new XText(node as XText);
                            break;
                        case XmlNodeType.CDATA:
                            yield return new XCData(node as XCData);
                            break;
                        case XmlNodeType.Comment:
                            yield return new XComment(node as XComment);
                            break;
                        case XmlNodeType.ProcessingInstruction:
                            yield return new XProcessingInstruction(node as XProcessingInstruction);
                            break;
                    }
                }
            }


    For your sample input, it will output

    <node_1_list:nodes xmlns:node_1_list="xyz/schemas/node_1_list">
      <node_1:node name="r2l3e4vm4" hostname="r2l3e4vm4" guid="000000505688128b0000000300000001" xmlns:node_1_list="xyz/schemas/node_1_list" xmlns:node_1="xyz/schemas/node_1" xmlns:attribute_1="xyz/schemas/attribute_1">
        <attribute_1:attribute name="DeviceType">Server</attribute_1:attribute>
        <attribute_1:attribute name="DeviceSubType">VMGuest</attribute_1:attribute>
        <attribute_1:attribute name="DeviceSubType">x86System</attribute_1:attribute>
        <attribute_1:attribute name="CIM_NetworkAdapter.creationClassName">CIM_NetworkAdapter</attribute_1:attribute>
      </node_1:node>
    </node_1_list:nodes>






    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 25, 2010 7:02 AM