0

I had been reading various tutorials but couldn't figure out to conditionally parse the following XML structure, where user inputs name of a State and then receive the name of the Capitol.

When I test it with the following code, I get no MessageBox popped up.

C# code

XDocument xd = XDocument.Load("Foo.xml");

foreach (var state in xd.Descendants("State"))
{
      Messagebox.Show(state.Attribute("Name").Value);
}

Foo.xml

<Main>
   <Title></Title>
   <Planet Name="Earth">
      <Continent Name="North America">
         <Country Name="USA">
            <State Name="Illinois" Capital="Springfield"></State>
            <State Name="Alabama" Capital="Montgomery"></State>
            ...
         </Country>
         <Country Name="Canada">
            <State Name="Alberta" Capital="Edmonton"></State>
            <State Name="British Columbia" Capital="Victoria"></State>  
            <State Name="Manitoba" Capital="Winnipeg"></State>
            ....
         </Country>
         <Country> ... </Country>
         <Country> ... </Country>
         <Country> ... </Country>
      </Continent>
   </Planet>
</Main>
3
  • 1
    are you missing a where clause in your linq? Commented Aug 2, 2017 at 8:04
  • Will you only have countries that are subdivided into states in your XML? I live in a (small) country that has no states and many address entry forms on the internet asks for my state which I cannot provide. And what do you want to return when different countries have states with the same name? I don't know if two such states exists but before The Soviet Union were split into independent countries Georgia was a state in The Soviet Union so it is possible. Commented Aug 2, 2017 at 8:14
  • @Hybridzz: I edited my test code to something simpler. Commented Aug 2, 2017 at 8:17

2 Answers 2

1

This should work:

List<State> states = (from s in XDocument.Load(@"Foo.xml").Descendants("State")
    select new State
    {
        Name = s.Attribute("Name").Value,
        Capital = s.Attribute("Capital").Value
    }).ToList();

foreach (State state in states)
{
    //do something...
}

//Or get a specific state
var alabama = states.FirstOrDefault(a => a.Name == "Alabama");
Sign up to request clarification or add additional context in comments.

Comments

1

I have modified your code a bit. This will return you the capitol from the state.

var stateCapital = from states in XDocument.Load(@"foo.xml").Descendants("State")
                    where states.Attribute("Name").Value == "Alabama"
                    select states.Attribute("Capital").Value;

Messagebox.Show(stateCapital);

If you need the names of all the Capitals for every state in a for loop-

var statesCollection = from states in XDocument.Load(@"foo.xml").Descendants("State") select states;

foreach (var state in statesCollection)
{
    Messagebox.Show(state.Attribute("Capital").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.