问题
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