跳过字符串,如果无效

发布于 2025-02-13 20:40:33 字数 1204 浏览 1 评论 0原文

我正在编写一个调用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你没皮卡萌 2025-02-20 20:40:35

如果发生错误,您可以尝试在XML响应中获取一些信息。这样的事情:

XmlNodeList nodes = root.SelectNodes("/Response/Detail/Data");
if (nodes == null)
{
    // Look the root content to see if contains a node with the error. 
    // In that case, do a SelectSingleNode. Here is an example with a 
    // sample path (is not real, I don't know anything about the XML 
    // error response schema)
    XmlNode node = root.SelectSingleNode("/Response/Detail/Error");
    var error = node?.InnerText;
    if (string.IsNullOrEmpty(error))
    {
        // If for any reason we can't get any error...
        error = $"Unknown error on query: {ConNo}";
    }
}
else
{
    foreach (XmlNode node in nodes)
    {
        // ...
    }
}

You can try to get some information in the XML response in case of error. Something like this:

XmlNodeList nodes = root.SelectNodes("/Response/Detail/Data");
if (nodes == null)
{
    // Look the root content to see if contains a node with the error. 
    // In that case, do a SelectSingleNode. Here is an example with a 
    // sample path (is not real, I don't know anything about the XML 
    // error response schema)
    XmlNode node = root.SelectSingleNode("/Response/Detail/Error");
    var error = node?.InnerText;
    if (string.IsNullOrEmpty(error))
    {
        // If for any reason we can't get any error...
        error = 
quot;Unknown error on query: {ConNo}";
    }
}
else
{
    foreach (XmlNode node in nodes)
    {
        // ...
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文