LINQ to XML: parsing simple XML entry

selvap

New Member
First time with LINQ to XML, climbing the learning curve...I am looking for the smoothest way how to retrieve and convert the data from XML file, with as little if-else and, god forbid, try-catch statements as possible. I really hate all that lengthy code just to retrieve single value from the doc.Having said this, I am looking for a fail-safe solution, that would work not break if the structure of the XML doc has unexpectedly changed. In such case, I'd expect that null value is retrieved so I could check for it later in code.Here's my XML doc:\[code\]?<?xml version="1.0" encoding="utf-8"?><Entry> <IntField>11</IntField> <StringField>String data</StringField> <DateTimeField>28/03/2013 18:10:02</DateTimeField></Entry>\[/code\]I am looking for code where I retrieve one value per row of code, ie:\[code\]myXMLdoc = XDocument.Load("sourceFile.xml");int? myIntField = (smart linq query that retrieves&converts the value, with fallback of null);string? myStringField = (smart linq query etc.);DateTime? myDateTimeField = (smart linq query etc.);if (myIntField == null){ // Complain that structure of the XML doc is bad}\[/code\]Perhaps my expectations that LINQ can do this are na?ve, but, well, having written lumps and lumps of XPath expressions and if-else statements for trivial pretty XML processing, I have accrued some bitterness about it. So I turn to LINQ with big hopes indeed.
 
Back
Top