C# breaking out of XML reading function if XML file is no longer available

混江龙づ霸主 提交于 2020-12-12 15:38:25

问题


I have a function that reads an XML page from a device attached to the network. The page is reached via url. The problem I'm having is that if I remove the device from the network while the function is parsing the XML page, the code freezes forever. How can I break out of the function if this happens? This is the code (the function returns a list of tags <example>):

public static List<string> GetTags(string xmlLocation) {    //xmlLocation = http://192.x.x.x/xmlpage
    List<string> dataList = new List<string>();                                             
    XmlTextReader reader = new XmlTextReader(xmlLocation);                                                                                                  
    while (reader.Read())                                                                   
    {
        switch (reader.NodeType)
        {
            case XmlNodeType.Element:                                                       
                dataList.Add(lastHeader);                                                    
                break;                                                                                                                                            
        }
    }
    reader.Dispose();
    return dataList;
}

来源:https://stackoverflow.com/questions/63671605/c-sharp-breaking-out-of-xml-reading-function-if-xml-file-is-no-longer-available

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!