将XML反序列化为显示错误的对象列表<的xmlns = '' >没想到。 [英] Deserializing XML into list of object showing error < xmlns=''> was not expected.
问题描述
我正在从我的应用程序中使用SOAP服务,并且在我的代码中将XMl内容作为响应消耗.wsdl文件。
I am consuming SOAP services from my application and I get following XMl content as response consuming .wsdl file in my code.
<Vehicles>
<FourWheeler>
<Availability>
<Brand>Toyota</Brand>
<Model>Fortuner</Model>
<Country>Japan</Country>
<Cost>90000</Cost>
</Availability>
<Availability>
<Brand>Hyundai</Brand>
<Model>Elentra</Model>
<Country>South Korea</Country>
<Cost>75000</Cost>
</Availability>
<Availability>
<Brand>Volkswagan</Brand>
<Model>Polo</Model>
<Country>Gremany</Country>
<Cost>95000</Cost>
</Availability>
<Availability>
<Brand>Tata</Brand>
<Model>Nano</Model>
<Country>25000</Country>
<Cost></Cost>
</Availability>
</Outbound>
<TwoWheeler></TwoWheeler>
</Vehicles>
我设计了我的类以这种方式反序列化XML ::
I have designed my class to deserialise XML this way::
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace ConsoleApp2
{
[XmlRoot("Vehicles")]
public class Vehicles
{
[XmlElement("FourWheeler")]
public FourWheeler FourWheeler
{
get;
set;
}
}
[XmlRoot("FourWheeler")]
public class FourWheeler
{
[XmlElement("Availability")]
public Vehicles Availability
{
get;
set;
}
}
[XmlRoot("Availability")]
public class Availability
{
[XmlElement("Brand")]
public string Brand
{
get;
set;
}
[XmlElement("Model")]
public string Model
{
get;
set;
}
[XmlElement("Country")]
public string Country
{
get;
set;
}
[XmlElement("Cost")]
public string Cost
{
get;
set;
}
}
}
我要将XML反序列化为列表的代码是:
My code to deserialize XML to list is :
XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Vehicles>));
System.IO.StreamReader sr = new System.IO.StreamReader("E:\\xmlTier.txt");
List<Vehicles> flightavailabilitylst = (List<Vehicles>)xmlSerializer.Deserialize(sr);
我收到错误
I am getting error in line
List<Vehicles> flightavailabilitylst = (List<Vehicles>)xmlSerializer.Deserialize(sr);
:
:
System.InvalidOperationException: 'There is an error in XML document (1, 2).'
InvalidOperationException: <Vehicles xmlns=''> was not expected.
发生了什么错误以及将XML转换为对象列表的正确方法???
我尝试过:
查看XML,层次结构是车辆> FourWheeler>可用性列表...所以我添加了代码:
What is happening wrong and what is right approach to convert XML into list of object???
What I have tried:
Looking into XML, hierarchy is Vehicles > FourWheeler > List of Availability... So I added code:
XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "Vehicles";
xRoot.IsNullable = true;
这对我没有帮助。
This did not help me.
推荐答案
你的xml无效。替换
Your xml isn't valid. Replace
</Outbound>
<TwoWheeler></TwoWheeler>
with
with
</FourWheeler>
XML表示单个车辆节点而不是它们的列表,因此相应地更改代码
The XML represents a single Vehicles node not a list of them so change the code accordingly
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Vehicles));
System.IO.StreamReader sr = new System.IO.StreamReader( ... );
Vehicles flightavailabilitylst = (Vehicles)xmlSerializer.Deserialize(sr);
另外可用性列表可用对象,而不是一个Vehicle对象,所以也改变了
Also Availability is a list of Available objects, not a single Vehicles object so change that also
[XmlRoot("FourWheeler")]
public class FourWheeler
{
[XmlElement("Availability")]
public List<Availability> Availability
{
get;
set;
}
}
您的FourWheeler和Outbound标签不匹配。
即您的XML无效。
Your "FourWheeler" and "Outbound" tags are "not matched".
i.e. your XML is invalid.
<Vehicles>
<FourWheeler>
<Availability>
<Brand>Toyota</Brand>
<Model>Fortuner</Model>
<Country>Japan</Country>
<Cost>90000</Cost>
</Availability>
<Availability>
<Brand>Hyundai</Brand>
<Model>Elentra</Model>
<Country>South Korea</Country>
<Cost>75000</Cost>
</Availability>
<Availability>
<Brand>Volkswagan</Brand>
<Model>Polo</Model>
<Country>Gremany</Country>
<Cost>95000</Cost>
</Availability>
<Availability>
<Brand>Tata</Brand>
<Model>Nano</Model>
<Country>25000</Country>
<Cost></Cost>
</Availability>
</FourWheeler>
<TwoWheeler></TwoWheeler>
</Vehicles>
XML已更新。但仍然显示相同的错误。
XML updated. but still showing same error.
这篇关于将XML反序列化为显示错误的对象列表<的xmlns = '' >没想到。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!