我正在尝试创建一个Web应用程序,该应用程序获取Web服务的响应(http://servicos.cptec.inpe.inpe.br/xml/listacidades(并将其存储在列表中城市,然后返回带有城市列表的行动。
<cidades>
<cidade>
<nome>São Benedito</nome>
<uf>CE</uf>
<id>4750</id>
</cidade>
<cidade>
<nome>São Benedito do Rio Preto</nome>
<uf>MA</uf>
<id>4751</id>
</cidade>
<cidade>
<nome>São Benedito do Sul</nome>
<uf>PE</uf>
<id>4752</id>
</cidade>
<cidade>
<nome>São Bentinho</nome>
<uf>PB</uf>
<id>5845</id>
</cidade>
<cidade>
<nome>São Bento</nome>
<uf>MA</uf>
<id>4753</id>
</cidade>
<cidade>
<nome>São Bento</nome>
<uf>PB</uf>
<id>4754</id>
</cidade>
<cidade>
<nome>São Bento Abade</nome>
<uf>MG</uf>
<id>4755</id>
</cidade>
Cidade类:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Xml.Serialization;
namespace WebApplication1.Models
{
[Serializable()]
public class Cidade
{
[XmlElement("nome")]
public string nome { get; set; }
[XmlElement("uf")]
public string uf { get; set; }
[XmlElement("id")]
[Key]
public int id { get; set; }
}
}
cidadecollection类:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Serialization;
namespace WebApplication1.Models
{
[Serializable()]
[XmlRoot("cidades")]
public class CidadeCollection
{
[XmlArrayItem(typeof(Cidade))]
public List<Cidade> cidades = new List<Cidade>();
}
}
cidadecollection控制器类:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Mvc;
using WebApplication1.Models;
using System.Xml.Serialization;
namespace WebApplication1.Controllers
{
public class CidadeCollectionController : Controller
{
// GET: CidadeCollection
public ActionResult Index(string s="")
{
string conteudo;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://servicos.cptec.inpe.br/XML/listaCidades?city=" + s);
request.Method = "GET";
WebResponse response = request.GetResponse();
CidadeCollection c = new CidadeCollection();
c.cidades = new List<Cidade>();
XmlSerializer ser;
StreamReader reader;
ser = new XmlSerializer(typeof(List<Cidade>));
reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
c.cidades = (List<Cidade>)ser.Deserialize(reader);
return View(c.cidades);
}
}
}
这是我遇到的错误:
在此处输入图像描述
好吧,这是一种简单的方法(琐碎的示例,根据需要改进(:
在您的Controller
中:
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
public ActionResult Foo()
{
var xml = XDocument.Load("http://servicos.cptec.inpe.br/XML/listaCidades");
var nodes = xml.Descendants("nome");
var xElements = nodes as XElement[] ?? nodes.ToArray();
var foo = new List<string>();
xElements.ForEach(element => foo.Add(element.Value));
ViewData["Nomes"] = new SelectList(foo);
return View();
}
在您的View
<h2>Foo</h2>
@Html.DropDownList("Nomes")
hth ..
如何获得" Cidade"的其他属性?像ID和UF一样存储在" Cidade"列表中?
给定模型:
public class Cidade
{
public string nome { get; set; }
public string uf { get; set; }
public int id { get; set; }
}
像这样对上述调整:
var xml = XDocument.Load("http://servicos.cptec.inpe.br/XML/listaCidades");
//all the cidade elements
var nodes = xml.Descendants("cidade");
var xElements = nodes as XElement[] ?? nodes.ToArray();
var foo = new List<Cidade>();
//iterate through the cidade elements and map them to your model
Array.ForEach(xElements, element =>
{
foo.Add(new Cidade
{
id = int.Parse(element.Element("id").Value), //or TryParse
nome = element.Element("nome").Value,
uf = element.Element("uf").Value
});
});
//foo now has your List (of Cidade)
//do whatever you need to do with it....
foo.ForEach(c => Console.WriteLine($"{c.id} | {c.nome} | {c.uf}"));
hth ...