我正在从我创建的站点下载一个 XML 文件,它在模拟器上运行良好;但是,它在电话上根本不起作用。它返回一个 Web 异常错误和一个 IO 错误...并且 HttpsCompleted 事件的错误属性表示错误远程服务器返回了一个错误。文件未找到。但这适用于我的模拟器。
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
WebClient wc = new WebClient();
wc.DownloadStringCompleted += HttpsCompleted;
wc.DownloadStringAsync(new Uri("http://.../SessionInfo.xml"));
}
private void HttpsCompleted(object sender, DownloadStringCompletedEventArgs e)
{
XDocument doc = null;
string results = null;
if (e.Error == null)
{
XDocument xdoc = XDocument.Parse(e.Result, LoadOptions.None);
List<XElement> xelem = xdoc.Root.Elements() as List<XElement>;
results = e.Result;
var sessions = from x in xdoc.Descendants("Session")
select new
{
ID = x.Descendants("ID").First().Value,
TITLE = x.Descendants("Title").First().Value,
TIME = x.Descendants("Time").First().Value,
DESCRIPTION = x.Descendants("Description").First().Value
};
foreach (var wd in sessions)
{
sessionsList.Add(new Session(wd.ID, wd.TITLE, wd.TIME, wd.DESCRIPTION));
Debug.WriteLine("Session ID is {0}, Title is {1}, Time is {2}", wd.ID, wd.TITLE, wd.TIME);
}
}
SessionInfoList.ItemsSource = sessionsList;
XML 看起来像:
<request><Session><ID>1234-1234-1234-1234</ID><Title>Session Title</Title><Time>10:00AM-11:30AM</Time><Description>Some description.</Description></Session></request>
最佳答案
首先,像这样的代码应该被禁止,因为你有 NullReferenceException 的风险:
TITLE = x.Descendants("Title").First().Value
然后,模拟器使用您计算机的互联网连接,因此如果您使用与手机相同的连接,则问题与您的手机有关,与您的代码无关。
关于c# - XML 下载适用于模拟器,但不适用于手机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8257568/