说我有这样的东西
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>是否可以使用YAXLib将其反序列化为如下所示
public class Note
{
public string to {get; set;}
public string from {get; set;}
public string Info {get; set;}
}
public class Info
{
public string header {get; set;}
public string body {get; set;}
}是否有任何设置可以让我更改这个路径以使其进入我设置的C#类?
发布于 2022-12-02 00:05:05
假设您想让这些类看起来如下所示:
public class Note
{
public string to { get; set; }
public string from { get; set; }
public Info Info { get; set; }
}
public class Info
{
public string heading { get; set; }
public string body { get; set; }
}看起来,获得所需内容的绝对最简单的方法是有两个单独的反序列化器。一个用于Info,另一个用于Note,然后反序列化(跳过缺少元素的错误)并自己将两个对象缝合在一起,如下所示:
Note GetNoteFromXml(string xml)
{
var noteSer = new YAXSerializer<Note>(new SerializerOptions
{
ExceptionHandlingPolicies = YAXExceptionHandlingPolicies.DoNotThrow
});
var infoSer = new YAXSerializer<Info>();
var note = noteSer.Deserialize(xml);
var info = infoSer.Deserialize(xml);
note.Info = info;
return note;
}var xml = @"<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>";
var note = GetNoteFromXml(xml);https://stackoverflow.com/questions/74648080
复制相似问题