跳过字符串,如果无效
我正在编写一个调用API,接收XML并显示跟踪的程序,同时显示了使用跟踪信息打开Web浏览器。字符串将为null
,如何显示错误而不是抛出null参考异常?
//ONLINE PORTAL TRACKING
string ConNo = Con.Text;
XmlDocument doc1 = new XmlDocument();
doc1.Load(url1 + ConNo + url2 + APIKEY);
XmlElement root = doc1.DocumentElement;
XmlNodeList nodes = root.SelectNodes("/Response/Detail/Data");
foreach (XmlNode node in nodes)
{
string ConsignmentNumber = node["ConNo"].InnerText;
string tracking_url = node["tracking_url"].InnerText;
string Lifts = node["Lifts"].InnerText;
string TrackingID = node["TrackingID"].InnerText;
string JobStatus = node["Status"].InnerText;
string Barcodes = node["Barcodes"].InnerText;
string Weight = node["Weight"].InnerText;
string DateMan = node["DateEntered"].InnerText;
this.ConsignmentNumber.Text = ConsignmentNumber;
this.Lifts.Text = Lifts;
// this.TrackingURL.Text = tracking_url;
this.TrackingID.Text = TrackingID;
this.JobStatus.Text = JobStatus;
this.Barcodes.Text = Barcodes;
this.Weight.Text = Weight;
this.DelMan.Text = DateMan;
System.Diagnostics.Process.Start(PortalURL1 + TrackingID);
I am writing a program that calls an API, receives XML and displays the tracking aswell as opening a web browser with tracking info in. I have a problem, if the wrong tracking id is entered the nodes return null
and the string will be null
, how can I display an error rather than throwing a null ref exception?
//ONLINE PORTAL TRACKING
string ConNo = Con.Text;
XmlDocument doc1 = new XmlDocument();
doc1.Load(url1 + ConNo + url2 + APIKEY);
XmlElement root = doc1.DocumentElement;
XmlNodeList nodes = root.SelectNodes("/Response/Detail/Data");
foreach (XmlNode node in nodes)
{
string ConsignmentNumber = node["ConNo"].InnerText;
string tracking_url = node["tracking_url"].InnerText;
string Lifts = node["Lifts"].InnerText;
string TrackingID = node["TrackingID"].InnerText;
string JobStatus = node["Status"].InnerText;
string Barcodes = node["Barcodes"].InnerText;
string Weight = node["Weight"].InnerText;
string DateMan = node["DateEntered"].InnerText;
this.ConsignmentNumber.Text = ConsignmentNumber;
this.Lifts.Text = Lifts;
// this.TrackingURL.Text = tracking_url;
this.TrackingID.Text = TrackingID;
this.JobStatus.Text = JobStatus;
this.Barcodes.Text = Barcodes;
this.Weight.Text = Weight;
this.DelMan.Text = DateMan;
System.Diagnostics.Process.Start(PortalURL1 + TrackingID);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果发生错误,您可以尝试在XML响应中获取一些信息。这样的事情:
You can try to get some information in the XML response in case of error. Something like this: