c# - Populating an object from an XmlNode -
i have object 'autotext' being populated xmlnode (retrieved remote source) following ctor:
public autotext(xmlnode autotextnode) { if (null == autotextnode) { throw new system.exception("attempted create autotext null autotextnode"); } foreach (xmlnode childnode in autotextnode.childnodes) { string childnodename = childnode.name; if (childnodename == "id") { this.autotextid = childnode.innertext; } ... snip ... else if (childnodename == "autotext") { this.autotextcontent = childnode.innertext; } } }
is there better way of doing this? know like:
this.autotextid = autotextnode["id"].innertext;
but presumably throw null ref exception if 'id' didn't exist, need checking on optional fields. on fields, presumably want specific exception if mandatory node didn't exist. solution seem ugly. i'm sure there better way, don't know is!
you can search "serialize xml object", there lot of question, topic that.
this answer : c# serialize xml object
Comments
Post a Comment