英文:
Import XML with a non typical format to a class
问题
我需要导入一个看起来像这样的XML文件:
<Carpark>
<Trucks>
<Truck brand='Chrysler' kg='2100'/>
</Trucks>
<Cars>
<Car brand='Mercedes' kg='1100'/>
</Cars>
<Tractors>
<Tractor brand='John Deere' kg='1500'/>
</Tractors>
</Carpark>
基本上,我想要有一个这样的Carpark
列表:
public class Carpark
{
// Type can be for example Truck, Car
public string Type { get; set; }
public string Brand { get; set; }
public int Weight { get; set; }
}
我不明白如何将这个XML文件转换成我想要的类。
我尝试过:
XElement xElement = XElement.Load(filePath);
IEnumerable<Carpark> carpark = xElement.Elements();
foreach(var vehicle in carpark)
{
// 一旦我写出这行,我对接下来该怎么做感到困惑
Console.WriteLine(vehicle.ToString());
}
英文:
I need to import a XML file looking like this:
<Carpark>
<Trucks>
<Truck brand='Chrysler' kg='2100'/>
</Trucks>
<Cars>
<Car brand='Mercedes' kg='1100'/>
</Cars>
<Tractors>
<Tractor brand='John Deere' kg='1500'/>
</Tractors>
</Carpark>
Essentially I want to have a list of Carpark
like this:
public class Carpark
{
// Type can be for example Truck, Car
public string Type { get; set; }
public string Brand { get; set; }
public int Weight { get; set; }
}
I don't understand how to convert this XML file to the class I want.
What I have tried is:
XElement xElement = XElement.Load(filePath);
IEnumerable<Carpark> carpark = xElement.Elements();
foreach(var vehicle in carpark)
{
// Once I wrote out the line I was confused as to how to proceed
Console.WriteLine(vehicle.ToString());
}
答案1
得分: 1
以下是您要翻译的内容:
使用Linq_To_XML和我相信还可以使用NewtonsoftJSON来完成这个操作:
void Main()
{
var xml = XElement.Parse(x)
.DescendantsAndSelf("Carpark")
.Descendants()
.Where(xe => xe.HasAttributes)
.Select(xe => new Carpark {
Type = xe.Name.ToString(),
Brand = (string)xe.Attribute("brand"),
Weight = (int)xe.Attribute("kg")
});
foreach (var cp in xml)
{
Console.WriteLine($"{cp.Type}, {cp.Brand}, {cp.Weight}");
}
}
static readonly string x = @"<Carpark>
<Trucks>
<Truck brand='Chrysler' kg='2100'/>
</Trucks>
<Cars>
<Car brand='Mercedes' kg='1100'/>
</Cars>
<Tractors>
<Tractor brand='John Deere' kg='1500'/>
</Tractors>
</Carpark>";
public class Carpark
{
// Type can be for example Truck, Car
public string Type { get; set; }
public string Brand { get; set; }
public int Weight { get; set; }
}
英文:
You can do that with Linq_To_XML and I believe with NewtonsoftJSON:
void Main()
{
var xml = XElement.Parse(x)
.DescendantsAndSelf("Carpark")
.Descendants()
.Where(xe => xe.HasAttributes)
.Select(xe => new Carpark {
Type = xe.Name.ToString(),
Brand = (string)xe.Attribute("brand"),
Weight = (int)xe.Attribute("kg")
})
;
foreach (var cp in xml)
{
Console.WriteLine($"{cp.Type}, {cp.Brand}, {cp.Weight}");
}
}
static readonly string x = @"<Carpark>
<Trucks>
<Truck brand='Chrysler' kg='2100'/>
</Trucks>
<Cars>
<Car brand='Mercedes' kg='1100'/>
</Cars>
<Tractors>
<Tractor brand='John Deere' kg='1500'/>
</Tractors>
</Carpark>";
public class Carpark
{
// Type can be for example Truck, Car
public string Type { get; set; }
public string Brand { get; set; }
public int Weight { get; set; }
}
答案2
得分: 0
你可以使用以下类来自动使用 XmlSerializer
进行反序列化:
public class Carpark
{
public List<Truck> Trucks { get; set; }
public List<Car> Cars { get; set; }
public List<Tractor> Tractors { get; set; }
}
[XmlInclude(typeof(Truck))]
[XmlInclude(typeof(Car))]
[XmlInclude(typeof(Tractor))]
public class Vehicle
{
[XmlIgnore]
public string Type => this.GetType().Name;
[XmlAttribute("brand")]
public string Brand { get; set; }
[XmlAttribute("kg")]
public int Weight { get; set; }
}
public class Truck : Vehicle
{
}
public class Car : Vehicle
{
}
public class Tractor : Vehicle
{
}
然后,你可以使用以下代码来进行反序列化:
var cp = (Carpark)serializer.Deserialize(someStreamReader);
如果需要更多帮助,请告诉我。
英文:
You can use the following classes to deserialize this with XmlSerializer
automatically
public class Carpark
{
public List<Truck> Trucks { get; set; }
public List<Car> Cars { get; set; }
public List<Tractor> Tractors { get; set; }
}
[XmlInclude(typeof(Truck))]
[XmlInclude(typeof(Car))]
[XmlInclude(typeof(Tractor))]
public class Vehicle
{
[XmlIgnore]
public string Type => this.GetType().Name;
[XmlAttribute("brand")]
public string Brand { get; set; }
[XmlAttribute("kg")]
public int Weight { get; set; }
}
public class Truck : Vehicle
{
}
public class Car : Vehicle
{
}
public class Tractor : Vehicle
{
}
var cp = (Carpark)serializer.Deserialize(someStreamReader);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论