2

I'm trying to deserialise some xml that contains multiple single xml elements in .Net C#, like so:

<Root>
 <Status>OK</Status>
 <Person>
  <Name>Element 1</Name>
 </Person>
 <Person>
  <Name>Element 2</Name>
 </Person>
</Root>

The Person nodes are not in a <Persons></Persons>, therefore I cannot use the [XmlArray] attribute.

Does anyone know to do that, without having to use the XPath with XDocument.

Thanks

1

1 Answer 1

1

If using .Net 3.5 or above, use Linq-to-XML:

string xml = "<root>...</root>";
XDocument doc = XDocument.Parse(xml); // Use .Load() if loading from a file
String status = doc.Root.Element("status").Value;
IEnumerable<string> personNames = doc.Root.Descendants("person").Select(x => x.Element("name").Value);
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.