1

I have a xml file like this

<Document>
   <Tests>
       <Test>
          <Name>A</Name>
          <Type>Like A</Type>
          <Members>
               <Member>1</Member>
               <Member>2</Member>
               <Member>3</Member>
          </Members>
       </Test>
       <Test>
          <Name>B</Name>
          <Type>Like B</Type>
          <Members>
               <Member>4</Member>
               <Member>5</Member>
               <Member>6</Member>
          </Members>
       </Test>
    </Tests>
</Document>

Now I made a class to hold data for each Test nodes:

public class TestData
{
public string Name {get;set;}
public string Type {get;set;}

public List<string> Members = new List<string>();

public AddMembers(List<string> members)
{
Members.AddRange(members);
}
}

Now I am trying to use xpath to fill my TestData class, but I have problem adding members to the List I have. I am trying to use LINQ but I cant do it yet :(

   public List<TestClass> GetTests()
    {
        List<TestClass> tests = new List<TestClass>();
        TestClass test;

        XPathNodeIterator it = nav.Select("/Document/Tests/Test");

        foreach (XPathNavigator val in it)
        {
            test= new TestClass();
            test.Name = val.SelectSingleNode(nav.Compile("Name")).Value;
            test.Type = val.SelectSingleNode(nav.Compile("Type")).Value;
            test.AddMembers(); //How can I return all the member nodes in a list?

            tests.Add(test);
        }


        return tests ;
    }

2 Answers 2

3

It's not clear why you're using XPathNodeIterator to start with, to be honest. If you can use LINQ to XML for this, it's really simple:

var list =  doc.Descendants("Test")
               .Select(x => TestClass.FromXElement(x))
               .ToList();

Where TestClass.FromXElement is something like:

public static TestClass FromXElement(XElement element)
{
    string name = (string) element.Element("Name");
    string type  = (string) element.Element("Type");
    List<string> members = element.Descendants("Member")
                                  .Select(x => x.Value)
                                  .ToList();
    return new TestClass(name, type, members);
}

(You don't have to use Descendants if you're rather control the hierarchy more strictly. For example, in TestClass.FromXElement you could use element.Element("Members").Elements("Member")

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Jon, Although your answer is nice and complete it is a bit hard for me to get it to work. At the moment I am scratching my head to figure out how to implement your answer, it is almost done!
1

Try this:

IEnumerable<string> members =
  from m in ((IEnumerable<XmlNode>)((IHasXmlNode)val).GetNode()
                                                .SelectNodes("Members/Member"))
    select m.Value;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.