XML 下载可以在模拟器中使用,但不能在手机上使用
我正在从我创建的站点下载 XML 文件,它在模拟器上运行良好;但是,它在手机上根本不起作用。返回时会出现 Web 异常错误和 IO 错误...并且 HttpsCompleted 事件的 error 属性表示错误,远程服务器返回了错误。未找到文件。但这适用于我的模拟器。
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>
I am downloading an XML file from a site I created and it works fine on the emulator; however, it doesn't work at all on the phone. It comes back with a web exception error and an IO error...and the error property from the HttpsCompleted event says error the remote server returned an error. File not Found. BUT this works on my emulator.
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 Looks like:
<request><Session><ID>1234-1234-1234-1234</ID><Title>Session Title</Title><Time>10:00AM-11:30AM</Time><Description>Some description.</Description></Session></request>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,应该禁止这样的代码,因为您面临 NullReferenceException 的风险:
然后,模拟器使用您计算机的互联网连接,因此如果您与手机使用相同的连接,则问题与您的手机有关,而不是与您的代码有关。
First, Code like this should be prohibited because you risk the NullReferenceException :
Then, emulator uses internet connection of your computer, so if you use the same connection with your phone the problem is related to your phone, not related to your code.