tags:

views:

37

answers:

1

My xml:

<Scenes>
    <Scene Index="0" Channel="71" Name="Scene1" />
    <Scene Index="1" Channel="71" Name="Scene2" />
    <Scene Index="2" Channel="71" Name="Scene3" />        
  </Scenes>

My code:

var elements = new List<List<string>>();
var attributtes = new List<string>();
XPathExpression expr1 = nav.Compile("//Scene/@*");
XPathNodeIterator iterator1 = nav.Select(expr1);
while (iterator1.MoveNext())
{
  XPathNavigator nav2 = iterator1.Current.Clone();
  attributtes.Add(nav2.Value);
}

This adds all my attributes from all elements to the list "attributtes".

But how do I add only the attributes to the list "attributtes", and then add that list to "elements"? So i get a list of elements which contains lists of elements. Hope I am clear enough about what i wish to accomplish..

+1  A: 

Here is your answer:

var elements = new List<List<string>>();

// this selects all 'Scene' elements
XPathNodeIterator elem = nav.Select("//Scene");
while(elem.MoveNext())
{
    XPathNavigator elemNav = elem.Current.Clone();

    var attributes = new List<string>();

    // now select all attributes of this particular 'Scene' element
    XPathNodeIterator attr = elemNav.Select("@*");
    while(attr.MoveNext())
    {
        attributes.Add(attr.Current.Value);
    }

    elements.Add(attributes);
}

This will create a list of elements, with a separate list of attributes for each element.

dacris