Refactor reading elements in an XML file - C# -
can refactor following piece of code reading elements in xml file:
if (!(xmldoc.element("element1").element("element2").element("element3").element("element4").element("element5").element("element6") == null)) { }
try use xpath
expression find element want, code submited can easly throw unexpected nullreferenceexception
don't want catch
.
something this:
if (xpath.evaluate("count(/element1/element2/element3/element4)", xmldoc) > 0) { }
ps. why negating expression of == null
? better readable , maintainable != null
without negation , trailing ()
in boolean expression.
Comments
Post a Comment