How can I reference array values ​​in string.Format?

advertisements

I am using XPath to exclude certain nodes within a menu. I want to expand on this to exclude nodes identified within an array.

This works to exclude all the nodes in the menu with id 2905 whose type is not content:

XmlNodeList nextLevelNodeList = currentNode
                                   .SelectNodes(string
                                                   .Format("
                                           Menu[not(MenuId = 2905)]
                                              /Item[
                                                 ItemLevel = {0}
                                                    and
                                                 ItemType != 'Javascript'
                                               ] |
                                           Menu[MenuId = 2905]
                                              /Item[
                                                 ItemLevel = {0}
                                                    and
                                                 ItemType = 'content'
                                               ]", iLevel));

What I'd like is to store the menuId and several others in an array and then reference that array within the string.Format function

Something like:

int[] excludeSubmenus = {2905, 323};
XmlNodeList nextLevelNodeList = currentNode
                                   .SelectNodes(string
                                                   .Format("
                                         Menu[not(MenuId in excludesubMenus)]
                                            /Item[
                                               ItemLevel={0}
                                                  and
                                               ItemType != 'Javascript'
                                             ] |
                                         Menu[MenuId in excludeSubMenus]
                                            /Item[
                                               ItemLevel={0}
                                                  and
                                               ItemType='content'
                                             ]", iLevel));

Any advice would be greatly appreciated!

ta Nathan

Edit - include example xml

<Item>
    <ItemId>322</ItemId>
    <ItemType>Submenu</ItemType>
    <ItemLevel>2</ItemLevel>
    <Menu>
        <MenuId>322</MenuId>
        <MenuLevel>2</MenuLevel>
        <Item>
            <ItemId>2905</ItemId>
            <ItemType>Submenu</ItemType>
            <ItemLevel>3</ItemLevel>
            <Menu>
                <MenuId>2905</MenuId>
                <MenuLevel>3</MenuLevel>
                <Item>
                    <ItemId>19196</ItemId>
                    <ItemType>content</ItemType>
                    <ItemLevel>4</ItemLevel>
                </Item>
                <Item>
                    <ItemId>19192</ItemId>
                    <ItemType>Submenu</ItemType>
                    <ItemLevel>4</ItemLevel>
                </Item>
            </Menu>
        </Item>
        <Item>
            <ItemId>2906</ItemId>
            <ItemType>Submenu</ItemType>
            <ItemLevel>3</ItemLevel>
            <Menu>
                <MenuId>323</MenuId>
                <MenuLevel>3</MenuLevel>
                <Item>
                    <ItemId>2432</ItemId>
                    <ItemType>content</ItemType>
                    <ItemLevel>4</ItemLevel>
                </Item>
                <Item>
                    <ItemId>12353</ItemId>
                    <ItemType>Submenu</ItemType>
                    <ItemLevel>4</ItemLevel>
                </Item>
            </Menu>
        </Item>
    </Menu>
</Item>


Use:

int[] excludeSubmenus = {2905, 323};

string notExpr = string.Empty;

for(int i=0; i < excludeSubmenus.Length; i++)
   {
    notExpr += string.Format("not(MenuId={0})", excludeSubmenus[i]);

    if(i != excludeSubmenus.Count-1)
       notExpr += " and ";
   } 

 XmlNodeList nextLevelNodeList =
    currentNode.SelectNodes(
       string.Format("//Menu[MenuId in excludeSubMenus]/Item
                              [ItemLevel={1} and not(ItemType='Javascript')]",
                      notExpr, iLevel)
                     );

Do note: In the above code the strings have been split into different lines to enhance readability. In ypur code you must not split any string , or use the string + (concatenation) operator to achieve the same effect.