简体   繁体   中英

Selecting specific node in xml c#

I made a program which to show LTE device information. The problem i faced is when trying to display only device name, this error occurs " Object reference not set to an instance of an object."

XML

 public void GetInfo()
    {
        Initialise();

        var Info = Get("api/device/information");

        LogMessage(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString());
        LogMessage(string.Format("Device name:\n {0}", Info.SelectSingleNode("//response/devicename").InnerText)); //something wrong here?

    }

Error :

System.NullReferenceException: Object reference not set to an instance of an object.
   at HuaweiRouterTool.HuaweiRouter.GetInfo() in C:\Users\pxc\Desktop\HuaweiRouterTool-master\HuaweiRouter.cs:line 233
   at HuaweiRouterTool.Program.DeviceInfo() in C:\Users\pxc\Desktop\HuaweiRouterTool-master\Program.cs:line 41
   at HuaweiRouterTool.Program.Main(String[] args) in C:\Users\pxc\Desktop\HuaweiRouterTool-master\Program.cs:line 21

But when i delete "devicename", it displayed all value.

public void GetInfo()
    {
        Initialise();

        var Info = Get("api/device/information");

        LogMessage(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString());
        LogMessage(string.Format("Device name:\n {0}", Info.SelectSingleNode("//response").InnerText));

    }

结果

what am i doing wrong here?

I solved this already.

Instead of "devicename" I changed to "DeviceName" and it works now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM