0

我正在保存一些来自 KML 文件的数据。为了有任何价值,我以这种方式创建了我的数据库。

create table PONTO
( 
    PontoID int not null identity, 
    ClienteID int null, 
    Descricao varchar(255) not null, 
    Latitude numeric(18,15) not null, 
    Longitude numeric(18,15) not null, 
    Altura decimal(12,2) null 
)

alter table PONTO add constraint PK_Ponto primary key (PontoID)

create table LINK
( 
    LinkID int not null identity, 
    PontoOrigemID int null, 
    PontoDestinoID int null, 
    LatitudeOrigem numeric(18,15) not null, 
    LongitudeOrigem numeric(18,15) not null, 
    LatitudeDestino numeric(18,15) not null, 
    LongitudeDestino numeric(18,15) not null, 
    Descricao varchar(255) not null, 
    Detalhes text null, 
    Altura decimal(12, 2) null, 
    Distancia decimal(12, 3) null, 
    AzimuteOrigemDestino decimal(3, 2) null, 
    AzimuteDestinoOrigem decimal(3, 2) null, 
    Velocidade varchar(50) null 
) 

alter table LINK add constraint PK_Link primary key (LinkID)

我试图创建一个浮动,但尝试加入时不起作用。

我保存数据的课程是这样的:

public class Ponto
{
    public int PontoID { get; set; }
    public int? ClienteID { get; set; }
    public string Descricao { get; set; }
    public Double Latitude { get; set; }
    public Double Longitude { get; set; }
    public Double? Altura { get; set; }
}

public class Link
{
    [Key]
    public int LinkID { get; set; }

    public int? PontoOrigemID { get; set; }
    public int? PontoDestinoID { get; set; }        
    public Double LatitudeOrigem { get; set; }
    public Double LongitudeOrigem { get; set; }
    public Double LatitudeDestino { get; set; }
    public Double LongitudeDestino { get; set; }

    public string Descricao { get; set; }
    public string Detalhes { get; set; }        

    public Double? Altura { get; set; }
    public Double? Distancia { get; set; }
    public Double? AzimuteOrigemDestino { get; set; }
    public Double? AzimuteDestinoOrigem { get; set; }
    public string Velocidade { get; set; }        
}

保存文件 KML:

public ActionResult CarregarArquivo()
{
    XmlDocument doc = new XmlDocument();
    doc.Load(Server.MapPath(@"~\AppData\doc.xml"));

    var dados = new List<Ponto>();
    Ponto ponto;
    Link link;

    /*  Existe duas pastas folder dentro do arquivo, a primeira com os pontos,
        segunda com os links */
    foreach (XmlNode nodePrincipal in doc.SelectNodes("/kml/Document/Folder"))
    {
        /* Pontos definidos */
        foreach (XmlNode nodePontos in nodePrincipal.SelectNodes("Placemark"))
        {
            ponto = new Ponto();
            ponto.Descricao = nodePontos.SelectSingleNode("name").InnerText; /* Nome do ponto */                    
            var coord = nodePontos.SelectSingleNode("Point/coordinates").InnerText.Split(',');
            ponto.Longitude = Convert.ToDouble(coord[0].Replace('.', ','));
            ponto.Latitude = Convert.ToDouble(coord[1].Replace('.', ','));

            using (var db = new TCC.Models.ERPContext())
            {
                db.Ponto.Add(ponto);
                db.SaveChanges();
            }
        }
        /* Verifica se tem outra pasta folder, que indica que é uma pasta de links */
        foreach (XmlNode nodeEnlace in nodePrincipal.SelectNodes("Folder"))
        {
            link = new Link();
            link.Descricao = nodeEnlace.SelectSingleNode("name").InnerText;

            foreach (XmlNode nodeEnlacePonto in nodeEnlace.SelectNodes("Placemark"))
            {
                if (nodeEnlacePonto.Attributes["id"] != null)
                {

                    if (link.LatitudeOrigem == 0.0 && link.LongitudeOrigem == 0.0)
                    {
                        link.LatitudeOrigem = Convert.ToDouble(nodeEnlacePonto.SelectSingleNode("LookAt/latitude").InnerText.Replace('.', ','));
                        link.LongitudeOrigem = Convert.ToDouble(nodeEnlacePonto.SelectSingleNode("LookAt/longitude").InnerText.Replace('.', ','));
                    }
                    else
                    {
                        link.LatitudeDestino = Convert.ToDouble(nodeEnlacePonto.SelectSingleNode("LookAt/latitude").InnerText.Replace('.', ','));
                        link.LongitudeDestino = Convert.ToDouble(nodeEnlacePonto.SelectSingleNode("LookAt/longitude").InnerText.Replace('.', ','));
                    }
                }
            }

            using (var db = new TCC.Models.ERPContext())
            {
                db.Link.Add(link);
                db.SaveChanges();
            }
        }
    }
    return RedirectToAction("Index");
}

问题是当我尝试抓取数据并且系统返回错误时:

无法使用值“十进制”设置“点”中的属性“纬度”。此属性必须设置为“Double”类型的非空值。

列出:

public JsonResult CarregaDados()
{
    using (var db = new ERPContext())
    {
        var dados = db.Ponto.ToList();
        return Json(dados.ToArray(), JsonRequestBehavior.AllowGet);
    }
}

我应该如何改进流程并使其发挥作用?

我使用 VS2008、SQL Server 2008、C# 和实体框架。

4

1 回答 1

2

这非常简单——您已经在 DB 模式中定义了一堆数字和十进制字段,而相应的 C# 类为这些字段提供了双精度字段——只需尝试更改为十进制。

于 2013-10-15T17:19:46.373 回答