XmlSerializer property converter

前端 未结 3 767
离开以前
离开以前 2020-12-03 17:57

Suppose we have a class which can be serialized/deserialized by XmlSerializer. It would be like so:

[XmlRoot(\"ObjectSummary\")]
public class Summary
{
              


        
3条回答
  •  一整个雨季
    2020-12-03 18:21

    using Newtonsoft.Json;
    
    [XmlRoot("ObjectSummary")]
    public class Summary
    {
         public string Name {get;set;}
         public string IsValid {get;set;}
    }
    
    
     //pass objectr of Summary class you want to convert to XML
     var json = JsonConvert.SerializeObject(obj);
     XNode node = JsonConvert.DeserializeXNode(json, "ObjectSummary");
    

    If you have more than one object, put it inside a list and Serialize List.

                    dynamic obj = new ExpandoObject();
                    obj.data = listOfObjects;
                    var json = JsonConvert.SerializeObject(obj);
                    XNode node = JsonConvert.DeserializeXNode(json, "ObjectSummary");
    

提交回复
热议问题