locked
Dynamically set XElement Select Conditions RRS feed

  • Question

  • User364803292 posted

    Hello

    I need to select a number of decendant elements from an xml document.

    Currently I have the following:

    myXElement.Descendants().
                    Where(x => x.Name == "MySubNodeName").ToList().
                        ForEach(x => myObjectList.Add(SetUpObjectFromNode(x)));

    What I need is to get the node names that set the where clause from a database or an app.config file.

    So I will have a list of Strings that must form the where conditions.

    eg. if the list of conditions returned from db or app.config is:

    List<String> conditions = new List<String>{"MySubNodeName1", "MySubNodeName2", "MySubNodeName3"}

    the select from myXElement must become:

    myXElement.Descendants().
                    Where(x => x.Name == "MySubNodeName1" || x.Name == "MySubNodeName2" || x.Name == "MySubNodeName3" || ).ToList().
                        ForEach(x => myObjectList.Add(SetUpObjectFromNode(x)));

    Is that possible?

    Monday, April 7, 2014 11:41 AM

Answers

  • User1208776063 posted

    So I will have a list of Strings that must form the where conditions.

    You can use Contains method to check if the name exists in the conditions array.

    myXElement.Descendants()
    	  .Where(x => conditions.Contains(x.Name))
    	  .ToList()
    	  .ForEach(x => myObjectList.Add(SetUpObjectFromNode(x)));
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 7, 2014 11:49 AM