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

Viewed 48

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;
}
0 Answers
Related