简体   繁体   中英

XML Serialize instead of JSON in .NET

Is there a way to fill out a class using XML data instead of 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.

You could use 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);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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