I have objects such as :
public class MyObject
{
public string OrderId { get; set; }
public string Installment { get; set; }
public string Amount { get; set; }
public List<Product> Products { get; set; }
}
public class Product
{
public string ProductCode { get; set; }
public string ProductName { get; set; }
public string Quantity { get; set; }
public string Price { get; set; }
}
I am trying to convert MyObject to XML with NewtonSoft.
I did not found directly convert an object to XML doc in NewtonSoft docs (NwetonSOftDocs)
So I thought, I can first convert MyObject to JSON then I can convert to XML but it did not work.
Helper Class:
public static XNode XmlSerialize<T>(T ToXml, string root, JsonSerializerSettings settings)
{
var ToJson = JsonConvert.SerializeObject(ToXml, Newtonsoft.Json.Formatting.Indented, settings);
return JsonConvert.DeserializeXNode(ToJson, root);
}
public static JsonSerializerSettings DefaultJsonSerializerSettings()
{
return new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver(),
DefaultValueHandling = DefaultValueHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore,
};
}
I tested in Console:
static void Main(string[] args)
{
XNode xml = Helper.XmlSerialize<MyObject>(new MyObject{ Amount = "100", OrderId = "123123123", Installment="1", Products = new List<Product> { new Product { ProductName = "Test", Price = "100" }, new Product { ProductName = "Test2", Price = "200" } } }, "auth", Helper.DefaultJsonSerializerSettings());
Console.WriteLine(xml.ToString());
Console.ReadKey();
}
Output:
<auth>
<orderId>123123123</orderId>
<installment>1</installment>
<amount>100</amount>
<products>
<productName>Test</productName>
<price>100</price>
</products>
<products>
<productName>Test2</productName>
<price>200</price>
</products>
</auth>
What i want :
<auth>
<orderId>123123123</orderId>
<installment>1</installment>
<amount>100</amount>
<products>
<product>
<productName>Test</productName>
<price>100</price>
</product>
<product>
<productName>Test2</productName>
<price>200</price>
</product>
</products>
</auth>
I'm waiting for your help.
CodePudding user response:
I always use System.Xml.Serialization . Is there any resctriction to not to use other libraries rather than NewtonSoft
static public void Serialize<T>(string filePath, T obj)
{
XmlSerializer writer = new XmlSerializer(typeof(T));
using (System.IO.StreamWriter file = new System.IO.StreamWriter(filePath))
{
writer.Serialize(file, obj);
}
}
static public void Deserialize<T>(string filePath, out T obj)
{
obj = default(T);
if (!File.Exists(filePath)) { throw new FileNotFoundException(filePath); }
XmlSerializer ser = new XmlSerializer(typeof(T));
using (XmlReader reader = XmlReader.Create(filePath))
{
obj = (T)ser.Deserialize(reader);
}
}
CodePudding user response:
Try this:
public string Serialize<T>(T obj)
{
XmlSerializer xsSubmit = new XmlSerializer(typeof(T));
using (var sww = new StringWriter())
{
using (XmlTextWriter writer = new XmlTextWriter(sww) { Formatting = Formatting.Indented })
{
xsSubmit.Serialize(writer, obj);
return sww.ToString();
}
}
}