0

I have got two arrays:

string[] Countries={"US","India","China","England","Australia"};
string[] States={"Texas","Karnataka","Xinjiang","Birmingham","Canbera"};

How do i make a LINQ to XML to present it in this way:

<Category>
  <US>Texas</US>
  <India>Karnataka</India>
  <China>Xinjiang</China>
  <England>Birmingham</England>
  <Australia>Canbera</Australia>
</Cateogry>

Thanks!

1 Answer 1

4

This is one possible way using LINQ Zip() method to combine the two arrays into XElements :

string[] Countries = { "US", "India", "China", "England", "Australia" };
string[] States = { "Texas", "Karnataka", "Xinjiang", "Birmingham", "Canbera" };
var doc = new XElement("Category",
                       Countries.Zip(States, (c, s) => new XElement(c, s))
          );
Console.WriteLine(doc.ToString());

Dotnetfiddle Demo

output :

<Category>
  <US>Texas</US>
  <India>Karnataka</India>
  <China>Xinjiang</China>
  <England>Birmingham</England>
  <Australia>Canbera</Australia>
</Category>
Sign up to request clarification or add additional context in comments.

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.