我在服务器www.testsite.com/sample_file.xml上有一个xml文件
xml的结构就像这样
<mobileclients>
<clientitem>
<code>SXPFBD</code>
<api>http://SPFD.azurewebsites.net/APIs/</api>
</clientitem>
<clientitem>
<code>STYPFBD</code>
<api>http://SPFD.azurewebsites.net/APIs/</api>
</clientitem>
</mobileclients>
我喜欢使用xamarin跨平台应用程序检查xml中是否存在代码输入
我无法将文件保留为资源因为它会不断更新因此可以随时从在线xml文件中读取
答案 0 :(得分:0)
当然,如果可以,我建议切换到JSON,但仍然可以使用XML。
以下是我开发的应用的代码段。
public List<NewsItem> GetNewsItems()
{
var newsXml = XDocument.Load("http://xxxxxxxx.nl/index.php?page=news");
var newsItems = (from newsitem in newsXml.Descendants("newsitem")
select new NewsItem
{
Title = WebUtility.HtmlDecode(newsitem.Element("title").Value),
Message = newsitem.Element("message").Value,
Date = DateTime.ParseExact(newsitem.Element("date").Value, "dd-MM-yyyy HH:mm", CultureInfo.InvariantCulture), // TODO better error handling
User = newsitem.Element("user").Value,
Replies = Convert.ToInt32(newsitem.Element("replies").Value),
Url = newsitem.Element("budgetgamingurl").Value,
Views = Convert.ToInt32(newsitem.Element("views").Value)
}).ToList();
return newsItems;
}
如您所见,它从URL加载文档并在内存中处理XPath查询。
它有点粗糙,它可以使用一些错误处理,但你会得到基本的想法。