Abdul
Abdul

Reputation: 1476

getting specific parent nodes depending on child nodes XML in c# LINQ

I have a long XML which has parent node as sdnEntry and every parent has its child sdnType which define the type of entry. I am trying to get only nodes which has sdnType to Individual.

short sample of my xml is here;

<sdnEntry>
    <uid>6905</uid>
    <lastName>abc</lastName>
    <sdnType>Entity</sdnType> // type is entity

    <akaList>
    <aka>
        <uid>4741</uid>
        <type>a.k.a.</type>
        <category>strong</category>
        <lastName>ABC</lastName>
        <firstName>ABCCCC</firstName>
    </aka>
    <aka>
        <uid>4742</uid>
        <type>a.k.a.</type>
        <category>weak</category>
        <lastName>ADCS</lastName>
    </aka>
    </akaList>

    <nationalityList>
        <nationality>
        <uid>5416</uid>
        <country>XYZ</country>
        <mainEntry>true</mainEntry>
        </nationality>
    </nationalityList>
</sdnEntry>

<sdnEntry>
    <uid>6905</uid>
    <lastName>abc</lastName>
    <sdnType>Individual</sdnType> // type is individual

    <akaList>
    <aka>
        <uid>4741</uid>
        <type>a.k.a.</type>
        <category>strong</category>
        <lastName>ABC</lastName>
        <firstName>ABCCCC</firstName>
    </aka>
    <aka>
        <uid>4742</uid>
        <type>a.k.a.</type>
        <category>weak</category>
        <lastName>ADCS</lastName>
    </aka>
    </akaList>

    <nationalityList>
        <nationality>
        <uid>5416</uid>
        <country>XYZ</country>
        <mainEntry>true</mainEntry>
        </nationality>
    </nationalityList>
</sdnEntry>

<sdnEntry>
    <uid>6905</uid>
    <lastName>abc</lastName>
    <sdnType>Individual</sdnType>

    <akaList>
    <aka>
        <uid>4741</uid>
        <type>a.k.a.</type>
        <category>strong</category>
        <lastName>ABC</lastName>
        <firstName>ABCCCC</firstName>
    </aka>
    <aka>
        <uid>4742</uid>
        <type>a.k.a.</type>
        <category>weak</category>
        <lastName>ADCS</lastName>
    </aka>
    </akaList>

    <nationalityList>
        <nationality>
        <uid>5416</uid>
        <country>XYZ</country>
        <mainEntry>true</mainEntry>
        </nationality>
    </nationalityList>
</sdnEntry>

<sdnEntry>
    <uid>6905</uid>
    <lastName>abc</lastName>
    <sdnType>Entity</sdnType>

    <akaList>
    <aka>
        <uid>4741</uid>
        <type>a.k.a.</type>
        <category>strong</category>
        <lastName>ABC</lastName>
        <firstName>ABCCCC</firstName>
    </aka>
    <aka>
        <uid>4742</uid>
        <type>a.k.a.</type>
        <category>weak</category>
        <lastName>ADCS</lastName>
    </aka>
    </akaList>

    <nationalityList>
        <nationality>
        <uid>5416</uid>
        <country>XYZ</country>
        <mainEntry>true</mainEntry>
        </nationality>
    </nationalityList>
</sdnEntry>

my code is like this but I am getting error;

 var lXelements = XElement.Parse(xml);
 var lParentNode = "sdnEntry";
  if (lParentNode == "sdnEntry")
            {
 //lXelements = (XElement)lXelements.Descendants("sdnType").Where(x => x.Name.LocalName == "Individual");
                lXelements = (XElement)lXelements.Descendants("sdnType").Where(x => (string)x.Value == "Individual");
            }

I am getting casting error currently, I don't my this code will give me result as I want or not.

error:

Additional information: Unable to cast object of type 'WhereEnumerableIterator`1[System.Xml.Linq.XElement]' to type 'System.Xml.Linq.XElement'.

Upvotes: 3

Views: 2139

Answers (3)

Isaac Morris
Isaac Morris

Reputation: 346

You can try the following and see if it will help you: this does not use lambda expressions but does the same as Nkosi code above

 XElement xe = XElement.Parse(xml);
        IEnumerable<XElement> newlist = (from x in xe.Elements("sdnEntry")
                                         where x.Element("sdnType").Value == "Individual"
                                         select x);
       ///Then at this point newlist contains all xelement where the sdnType=Individual

Upvotes: 1

Nkosi
Nkosi

Reputation: 247088

The error is because you are trying to reassign the Linq Where result to an XElement.

That aside, you basically want to get all <sdnEntry> nodes that have a child <sdnType>Individual</sdnType>

XElement elements = XElement.Parse(xml);
var parentNode = "sdnEntry";
var childNode = "sdnType";
var childNodeValue = "Individual";
List<XElement> entries = elements
    .Descendants(parentNode)
    .Where(parent => parent.Descendants(childNode)
        .Any(child => child.Value == childNodeValue)
    ).ToList();

entries should contain only the desired parent elements that match the provided child element filter.

The approach above searched for child nodes based on the parent node.

The following approach finds the child nodes first and then looks up the tree for the parent node

List<XElement> entries = elements
    .Descendants(childNode)
    .Where(child => child.Value == childNodeValue)
    .SelectMany(child => child.Ancestors(parentNode))
    .ToList();

Both approaches produced the same 2 matching elements result based on the following XML

var xml = @"
<sdnList>
    <sdnEntry>
        <uid>6905</uid>
        <lastName>abc</lastName>
        <sdnType>Entity</sdnType>

        <akaList>
        <aka>
            <uid>4741</uid>
            <type>a.k.a.</type>
            <category>strong</category>
            <lastName>ABC</lastName>
            <firstName>ABCCCC</firstName>
        </aka>
        <aka>
            <uid>4742</uid>
            <type>a.k.a.</type>
            <category>weak</category>
            <lastName>ADCS</lastName>
        </aka>
        </akaList>

        <nationalityList>
            <nationality>
            <uid>5416</uid>
            <country>XYZ</country>
            <mainEntry>true</mainEntry>
            </nationality>
        </nationalityList>
    </sdnEntry>

    <sdnEntry>
        <uid>6905</uid>
        <lastName>abc</lastName>
        <sdnType>Individual</sdnType> 

        <akaList>
        <aka>
            <uid>4741</uid>
            <type>a.k.a.</type>
            <category>strong</category>
            <lastName>ABC</lastName>
            <firstName>ABCCCC</firstName>
        </aka>
        <aka>
            <uid>4742</uid>
            <type>a.k.a.</type>
            <category>weak</category>
            <lastName>ADCS</lastName>
        </aka>
        </akaList>

        <nationalityList>
            <nationality>
            <uid>5416</uid>
            <country>XYZ</country>
            <mainEntry>true</mainEntry>
            </nationality>
        </nationalityList>
    </sdnEntry>

    <sdnEntry>
        <uid>6905</uid>
        <lastName>abc</lastName>
        <sdnType>Individual</sdnType>

        <akaList>
        <aka>
            <uid>4741</uid>
            <type>a.k.a.</type>
            <category>strong</category>
            <lastName>ABC</lastName>
            <firstName>ABCCCC</firstName>
        </aka>
        <aka>
            <uid>4742</uid>
            <type>a.k.a.</type>
            <category>weak</category>
            <lastName>ADCS</lastName>
        </aka>
        </akaList>

        <nationalityList>
            <nationality>
            <uid>5416</uid>
            <country>XYZ</country>
            <mainEntry>true</mainEntry>
            </nationality>
        </nationalityList>
    </sdnEntry>

    <sdnEntry>
        <uid>6905</uid>
        <lastName>abc</lastName>
        <sdnType>Entity</sdnType>

        <akaList>
        <aka>
            <uid>4741</uid>
            <type>a.k.a.</type>
            <category>strong</category>
            <lastName>ABC</lastName>
            <firstName>ABCCCC</firstName>
        </aka>
        <aka>
            <uid>4742</uid>
            <type>a.k.a.</type>
            <category>weak</category>
            <lastName>ADCS</lastName>
        </aka>
        </akaList>

        <nationalityList>
            <nationality>
            <uid>5416</uid>
            <country>XYZ</country>
            <mainEntry>true</mainEntry>
            </nationality>
        </nationalityList>
    </sdnEntry>
</sdnList>
";

Upvotes: 5

Jens Meinecke
Jens Meinecke

Reputation: 2940

You are trying to cast IEnumerable<XElement> to XElement. Take away the cast and it should work:

lXelements = lXelements.Descendants("sdnType")
          .Where(x => (string)x.Value == "Individual");

...

foreach(var element in lXelements)
{
   DoSomething(element);
}

Upvotes: 0

Related Questions