The situation I came across is parsing an XML document into an object using Linq. During parsing, I check that the Elements are not null before proceeding with the analysis of their values. Is there a way to simplify this statement?
var variable = (from x in xdoc.Descendants("Root") select new AccountingResponse { NetCharge = x.Element("Charges") != null && x.Element("Charges").Element("NetCharge") != null ? x.Element("Charges").Element("NetCharge").Value : "0", TotalCharge = x.Element("Charges") != null && x.Element("Charges").Element("TotalCharge") != null ? x.Element("Charges").Element("TotalCharge").Value : "0" }).SingleOrDefault();
To summarize, I do not want to continue to check if nodes exist in each row. I know that I can check if a node exists before parsing, but there may be other data that needs to be analyzed to create AccountingResponse, and I want to avoid if instructions that process only part of XML at a time
Or maybe I'm doing it completely wrong, and there is a better way!
source share