尝试反序列化在浏览器中返回以下xml的休息服务uri(http:// localhost:8150 / getNames)时遇到问题。问题在于命名空间。
- <ArrayOfFoodTruck xmlns="Artybones" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
- <FoodTruck>
<Company i:nil="true" />
<DayOfWeek i:nil="true" />
<Location i:nil="true" />
<TimeOfDay i:nil="true" />
<TruckID>0</TruckID>
<TruckName>BBQ Smith</TruckName>
<Website>http://www.bbqtest.com</Website>
</FoodTruck>
- <FoodTruck>
<Company i:nil="true" />
<DayOfWeek i:nil="true" />
<Location i:nil="true" />
<TimeOfDay i:nil="true" />
<TruckID>0</TruckID>
<TruckName>Bon Me</TruckName>
<Website>http://www.trucktest.com</Website>
</FoodTruck>
有没有人知道如何将命名空间包含在以下c#代码中,以将xml反序列化为控制台应用程序中的对象。这里的问题同样是命名空间,因为我收到的错误是XML文档中存在错误(1,2)。内部异常是(FoodTruck xmlns ='Artybones'&gt;不是预期的。)
感谢。
public class FoodTruck
{
[XmlElement("Website")]
public string Website { get; set; }
[XmlElement("TruckName")]
public string TruckName { get; set; }
}
public class FoodTruckNames
{
[XmlElement("FoodTruck") ]
public FoodTruck[] FoodTruckers { get; set; }
}
static void DeserializeTheXML()
{
var url = "http://localhost:8150/getNames";
var serializer = new XmlSerializer(typeof(FoodTruckNames), new XmlRootAttribute("FoodTruck"));
using (var client = new WebClient())
using (var stream = client.OpenRead(url))
{
var ftNames = (FoodTruckNames)serializer.Deserialize(stream);
foreach (var x in ftNames.FoodTruckers)
{
Console.WriteLine(
"code: {0}, name: {1}",
x.TruckName,
x.Website
)
;
Console.ReadLine();
}
}
}