[英]XML Serialize instead of JSON in .NET
Is there a way to fill out a class using XML data instead of JSON? 有没有一种方法可以使用XML数据而不是JSON来填充类? Example in marc's excellent answer. 马克的出色回答中的例子。
I would like everything to be as close to that code except the input is an xml file instead of json. 我希望所有内容都尽可能接近该代码,只是输入内容是xml文件而不是json。
You could use XmlSerializer : 您可以使用XmlSerializer :
public class Foo
{
public string Bar { get; set; }
}
class Program
{
public static void Main()
{
var serializer = new XmlSerializer(typeof(Foo));
var xml = "<Foo><Bar>beer</Bar></Foo>";
using (var reader = new StringReader(xml))
{
var foo = (Foo)serializer.Deserialize(reader);
Console.WriteLine(foo.Bar);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.