private SmtpClient getServer()
{
return (from e in doc.Elements("emailsetting")
select new SmtpClient()
{
Host = e.Attribute("server").Value,
Port = Convert.ToInt32(e.Attribute("port").Value)
}).FirstOrDefault();
}
The xml config file:
<emailsetting>
<stmp server="10.182.182.182" port="25" />
<from address="ithelpdest@citics.com.hk"/>
<to address=""/>
<cc address=""/>
</emailsetting>
Why throws the exception:
NullReferenceException was unhandled
Object reference not set to an instance of an object.
I’m new to LINQ,plz help.
You are accessing only the
emailsettingelement, which doesn’t have an attribute namedserverorport.You need to get the attributes from the
smtpchild element.Try this: