简体   繁体   English

如何快速将XML反序列化为一种类型?

[英]How do I quickly deserialize XML into a type?

Suppose I have this XML as a string: 假设我将此XML作为字符串:

<calles>
  <calle>
   <nombre>CALLAO AV.</nombre>
   <altura>1500</altura>
   <longitud>-58.3918617027</longitud>
   <latitud>-34.5916734896</latitud>
   <barrio>Recoleta</barrio>
  </calle>
 </calles>

And and have this Type I created to map that XML: 并且让我创建了此类型以映射该XML:

public class Ubicacion
{
    public string Latitud { get; set; }
    public string Longitud { get; set; }
    public string Nombre { get; set; }
    public string Altura { get; set; }
    public string Barrio { get; set; }

    public Ubicacion() { }
}

I need to take that XML file and create an object with those values... 我需要获取该XML文件并使用这些值创建一个对象...

Does somebody know a quick way to do it? 有人知道一种快速的方法吗? with C#? 用C#? I have been trying this but is not working at all... 我一直在尝试这个,但是根本没有用...

XElement dir = XElement.Parse(text);

Ubicacion informacion = from d in dir.Elements("calle").
        select new Ubicacion
        {
           Longitud = d.Element("longitud").Value,
           Latitud = d.Element("latitud").Value,
           Altura = d.Element("altura").Value,
           Nombre = d.Element("nombre").Value,
           Barrio = d.Element("barrio").Value,
        };
return informacion.Cast<Ubicacion>();

Any ideas? 有任何想法吗?

Have you tried this? 你有尝试过吗?

 XElement dir = XElement.Parse(text); 

        var informacion = from d in dir.Elements("calle"). 
                          select new Ubicacion 
                          { 
                              Longitud = d.Element("longitud").Value, 
                              Latitud = d.Element("latitud").Value, 
                              Altura = d.Element("altura").Value, 
                              Nombre = d.Element("nombre").Value, 
                              Barrio = d.Element("barrio").Value, 
                          }.ToList();

Remember that the LINQ query isn't executed until you iterate through it. 请记住,直到您遍历LINQ查询才执行它。 The .ToList() extension method will do that for you. .ToList()扩展方法将为您完成此操作。 It will then produce a List<Ubicacion> collection. 然后它将产生一个List<Ubicacion>集合。

One way of doing it would be this: 一种实现方法是:

  • run your data.xml through xsd.exe twice - first using xsd.exe (filename).xml which will produce a XML schema for your XML file with the same name and a .xsd file extension 通过xsd.exe两次运行data.xml-首先使用xsd.exe (filename).xml ,这将为您的XML文件生成具有相同名称和.xsd文件扩展名的XML模式
  • secondly, run xsd.exe /c (filename).xsd which will produce a C# class corresponding to your XML 其次,运行xsd.exe /c (filename).xsd ,这将生成一个与您的XML相对应的C#类
  • now, you're able to easily deserialize the XML into a C# class 现在,您可以轻松地将XML反序列化为C#类
  • set up AutoMapper to easily map your generated/deserialized class into an instance of your target type - since both types have identical property names, setting up the AutoMapper is a breeze: 设置AutoMapper可以轻松地将生成/反序列化的类映射到目标类型的实例-由于两种类型的属性名称相同,因此设置AutoMapper很容易:

     Mapper.CreateMap<Deserialized, Ubicacion>(); Ubicacion target = Mapper.Map<Deserialized, Ubicacion>(your-deserialized-instance); 

    and that's all there is to do! 这就是所有要做的!

Depending on how many times you need to do this, that might be an easier way than manually parsing the XML into bits and pieces and assembling your new object instances based on that parsing. 取决于您需要执行此操作的次数,这可能比手动将XML解析成零碎的片段并根据该解析组装新的对象实例要容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM