C# Using struct in XmlSerializer without creating a separate xml node - c#

I have a class I want to serialize to an Xml:
[XmlRoot(ElementName = "readCase", Namespace = "XXX")]
public class ReadCase
{
[XmlElement(ElementName = "versionAsOf", Namespace = "XXX")]
public BaseUtcTimeStamp VersionAsOf { get; set; }
}
I created a struct BaseUtcTimeStamp:
[Serializable]
public struct BaseUtcTimeStamp
{
private string _utcTimestamp;
public string UtcTimestamp { get => _utcTimestamp; set { } } // set is needed for XmlSerializer
public BaseUtcTimeStamp(DateTime utcDateTime)
{
if (utcDateTime.Kind != DateTimeKind.Utc)
{
throw new ArgumentException("Given dateTime must be Utc.");
}
_utcTimestamp = utcDateTime.ToString("yyyy-MM-ddTHH:mm:ssZ");
}
}
The serialized XML looks like below:
<case:readCase>
<case:versionAsOf>
<case:UtcTimestamp>2021-07-01T07:38:14Z</case:UtcTimestamp>
</case:versionAsOf>
</case:readCase>
I want the BaseUtcTimeStamp type value to be directly within VersionAsOf (like a GUID). Example:
<case:readCase>
<case:versionAsOf>2021-07-01T07:38:14Z</case:versionAsOf>
</case:readCase>

[XmlText]
public string UtcTimestamp { get => _utcTimestamp; set { } }

Related

Deserialize XML File into DTO

I'm trying to import an XML file into a DTO.
Example Xml
<?xml version="1.0" encoding="UTF-16"?>
<treffer>
<prod_internid>123456789</prod_internid>
<md_nr>56642</md_nr>
<md_mart_id>4</md_mart_id>
<md_mart_bez>Twitter</md_mart_bez>
</treffer>
Xml Dtos
[XmlRoot("treffer")]
public class DeAnalyseArtikelXmlDto
{
[XmlElement("prod_internid")]
public long ArtikelId { get; set; }
[XmlElement("md_nr")]
public int MedienNr { get; set; }
public DeMedienArtXmlDto MedienArt { get; set; }
}
[XmlRoot("treffer")]
public class DeMedienArtXmlDto
{
[XmlElement("md_mart_bez")]
public string Bezeichnung { get; set; }
}
Importer:
public bool Import()
{
DeserializeXMLFileToObject<DeAnalyseArtikelXmlDto>("C:\\temp\\xml\\toImport.xml");
return true;
}
public static T DeserializeXMLFileToObject<T>(string XmlFilename)
{
T returnObject = default(T);
if (string.IsNullOrEmpty(XmlFilename)) return default(T);
try
{
StreamReader xmlStream = new StreamReader(XmlFilename);
XmlSerializer serializer = new XmlSerializer(typeof(T));
returnObject = (T)serializer.Deserialize(xmlStream);
}
catch (Exception ex)
{
// ExceptionLogger.WriteExceptionToConsole(ex, DateTime.Now);
}
return returnObject;
}
It works for the properties ArtikelId and MedienNr but the property DeMedienArtXmlDto stays null (even tho the properties of the DeMedienArtXmlDto are annotated themselves...)
Am I doing something wrong that it doesn't fill this property?
Thanks in advance
How should your program know, that md_mart_bez should serialize to MedienArt? You have to indicate this by adding the appropriate XmlElement:
[XmlRoot("treffer")]
public class DeAnalyseArtikelXmlDto
{
[XmlElement("prod_internid")]
public long ArtikelId { get; set; }
[XmlElement("md_nr")]
public int MedienNr { get; set; }
[XmlElement("md_mart_bez")]
public DeMedienArtXmlDto MedienArt { get; set; }
}
Instead of using a class that wraps a Bezeichnung, why not just put the string directly into your parent class? Alternativly use an enum that contains the different values for MedienArt.
public enum DeMedienArtXmlDto
{
[XmlEnum("md_mart_bez")]
public Art1
}

Azure Service-Fabric and DocumentDB message serialization issue

So, - in my DocumentDB I may have the following document:
{
"id": 1,
"type": "A",
"content": {
"x": 1,
"y": 2
}
}
That may be backed by this model:
public class acontent
{
public int x { get; set; }
public int y { get; set; }
}
public class document
{
public int id { get; set; }
public string type { get; set; }
public object content { get; set; }
}
public class documenta : document
{
public new acontent content { get; set; }
}
The idea here is that document is a complex object where content may vary depending on type.
Now, - in my ServiceFabric application I have a stateless microservice that reads from DocumentDB and should return a document type object when called from the ServiceProxy.
The problem in this is that the DocumentQuery from the DocumentDB SDK, uses Json.NET serializer when querying the database, whilst servicefabric uses DataContractSerializer for serializing the service-messages.
So when the content part of document class is being deserialized from the DocumentDB it becomes:
Newtonsoft.Json.Linq.JObject
But when it is serialized back through the returned service-message you get the exception:
Type 'Newtonsoft.Json.Linq.JToken' is a recursive collection data
contract which is not supported. Consider modifying the definition of
collection 'Newtonsoft.Json.Linq.JToken' to remove references to
itself.
To illustrate this issue try the folowing code:
using System;
using System.IO;
using System.Text;
using System.Runtime.Serialization.Json;
using Newtonsoft.Json;
namespace jsoinissue
{
public class acontent
{
public int x { get; set; }
public int y { get; set; }
}
public class document
{
public int id { get; set; }
public string type { get; set; }
public object content { get; set; }
}
public class documenta : document
{
public new acontent content { get; set; }
}
public class Program
{
private const string JSON_A = "{\"id\":1,\"type\":\"A\",\"content\":{\"x\":1,\"y\":2}}";
private static string SerializeObject<T> (T obj)
{
try
{
DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(T));
using (var ms = new MemoryStream())
{
js.WriteObject(ms, obj);
ms.Position = 0;
using (var sr = new StreamReader(ms))
return sr.ReadToEnd();
}
}
catch (Exception e)
{
return String.Format("EXCEPTION: {0}",e.Message);
}
}
public static void Main()
{
var A = JsonConvert.DeserializeObject<document>(JSON_A);
var a = SerializeObject<document>(A);//HERE BE TROUBLE
Console.WriteLine(a);
Console.ReadKey();
}
}
}
How could I best resolve this issue?
Your basic problem is that DataContractJsonSerializer does not support untyped, free-form JSON data. As explained in Working with untyped JSON in a WCF service the System.Json namespace was added to Silverlight for this purpose, but it seems that it never made it into the full .Net class library.
Instead, in your stateless microservice can do a nested serialization where the free-form JSON is represented as an escaped string literal when serializing using the data contract serializer. Thus your classes would look something like this:
[DataContract]
[JsonObject]
public abstract class documentbase
{
[DataMember]
[JsonProperty]
public int id { get; set; }
[DataMember]
[JsonProperty]
public string type { get; set; }
[IgnoreDataMember]
[JsonProperty("content")]
public abstract JToken JsonContent { get; set; }
[JsonIgnore]
[DataMember(Name = "content")]
string DataContractContent
{
get
{
if (JsonContent == null)
return null;
return JsonContent.ToString(Newtonsoft.Json.Formatting.None);
}
set
{
if (string.IsNullOrEmpty(value))
JsonContent = null;
else
JsonContent = JToken.Parse(value);
}
}
}
[DataContract]
[JsonObject]
public class document : documentbase
{
JToken content;
public override JToken JsonContent { get { return content; } set { content = value; } }
}
[DataContract]
[JsonObject]
public class document<T> : documentbase where T : class
{
[IgnoreDataMember]
[JsonIgnore]
public T Content { get; set; }
public override JToken JsonContent
{
get
{
if (Content == null)
return null;
return JToken.FromObject(Content);
}
set
{
if (value == null || value.Type == JTokenType.Null)
Content = null;
else
Content = value.ToObject<T>();
}
}
}
Then the JSON generated by SerializeObject<document>(A) will look like:
{
"content":"{\"x\":1,\"y\":2}",
"id":1,
"type":"A"
}
Then, on the receiving system, you can deserialize to a document using the data contract serializer, then query the deserialized JToken JsonContent with LINQ to JSON. Alternatively, if the receiving system knows to expect a document<acontent> it can deserialize the data contract JSON as such, since document and document<T> have identical data contracts.
Have you looked into changing away from DataContractSerializer to a serializer with better support instead? Here's how you'd plug in a different serializer.
class InitializationCallbackAdapter
{
public Task OnInitialize()
{
this.StateManager.TryAddStateSerializer(new MyStateSerializer());
return Task.FromResult(true);
}
public IReliableStateManager StateManager { get; set; }
}
class MyStatefulService : StatefulService
{
public MyStatefulService(StatefulServiceContext context)
: this(context, new InitializationCallbackAdapter())
{
}
public MyStatefulService(StatefulServiceContext context, InitializationCallbackAdapter adapter)
: base(context, new ReliableStateManager(context, new ReliableStateManagerConfiguration(onInitializeStateSerializersEvent: adapter.OnInitialize)))
{
adapter.StateManager = this.StateManager;
}
}
This could be newtonsoft or whatever. Also I believe that the method is currently marked "Deprecated" however there's no alternative, so if it solves your problem go ahead and use it.

Cannot parse simple XML into an object?

XML
<MeterWalkOrder>
<Name>Red Route</Name>
<Meters>
<Meter>
<MeterID>1</MeterID>
<SerialNumber>12345</SerialNumber>
</Meter>
<Meter>
<MeterID>2</MeterID>
<SerialNumber>SE</SerialNumber>
</Meter>
</Meters>
</MeterWalkOrder>
I cannot get simple XML into an object using any serializer
var xml = File.ReadAllText("WalkOrder.xml");
var xmlSerializer = new NFormats.Xml.XmlSerializer();
var obj = xmlSerializer.Deserialize<MeterWalkOrder>(new StringReader(xml));
I just get back 2 meter objects that have none of the attributes set and the name is not even set in walk order.
public partial class MeterWalkOrder
{
public MeterWalkOrder()
{
Meters = new List<Meter>();
}
[DataMember]
public String Name { get; set; }
}
}
using System;
using System.Xml.Serialization;
namespace WindowsFormsApplication1.Classes
{
public class Meter : IMeter
{
[XmlAttribute]
public int MeterID { get; set; }
[XmlAttribute]
public String SerialNumber { get; set; }
}
}
I am willing to try any xml serializer.
First of all i suggest you to read Introducing XML Serialization on MSDN
You made a couple of errors which lead to not mentioned exceptions thrown when you run your code.
in your Xml MeterID and SerialNumber are not attributes they are elements. (As Wyat Earp commented)
if you want to serialize something you have to tell that it should be [Serializable]
serialization requires an implemented public empty constructor
dont open streams when you are not closing them (use "using")
To test if your serialization works best first serialize, then check the output and then implement deserialize
Find a fully working example below:
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace X123
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
MeterWalkOrder mo = new MeterWalkOrder();
mo.Name = "name";
mo.Meters.Add(new Meter { MeterID = 1, SerialNumber = "kdkdkd" });
mo.Meters.Add(new Meter { MeterID = 2, SerialNumber = "holladrio" });
var xmlSerializer = new XmlSerializer(typeof(MeterWalkOrder), new Type[] { typeof(Meter) });
{
xmlSerializer.Serialize(File.CreateText("hello.xml"), mo);
using (Stream s = File.OpenRead("hello.xml"))
{
var obj = xmlSerializer.Deserialize(s);
}
}
}
}
[Serializable]
public class MeterWalkOrder
{
public MeterWalkOrder()
{
}
public string Name { get; set; }
public List<Meter> Meters { get { return meters; } set { meters = value; } }
private List<Meter> meters = new List<Meter>();
}
[Serializable]
public class Meter
{
public Meter()
{
}
[XmlAttribute]
public int MeterID { get; set; }
[XmlAttribute]
public string SerialNumber { get; set; }
}
I used your sample XML and generated the classes inside VisualStudio with the Paste Special -> Paste XML as Classes, modified them a little bit to make them more readable and got the following class definitions from it:
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public class MeterWalkOrder
{
public string Name { get; set; }
[System.Xml.Serialization.XmlArrayItemAttribute("Meter", IsNullable = false)]
public List<MeterWalkOrderMeter> Meters { get; set; }
}
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public class MeterWalkOrderMeter
{
public int MeterID { get; set; }
public string SerialNumber { get; set; }
}
using the above classes and the below code, it generated the objects without errors.
string inputXml = File.ReadAllText(#"C:\Temp\SOTest.xml");
//using System.Xml.Serialization;
System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(MeterWalkOrder));
MeterWalkOrder outputObject = xmlSerializer.Deserialize(new StringReader(inputXml)) as MeterWalkOrder;

WebAPI List<T> Serialization XML/JSON output

I tried to create a method in a ApiController that looks like this:
public DemoList<Demo> GetAll()
{
var result = new DemoList<Demo>() { new Demo(){Y=2}, new Demo(), new Demo(){Y=1} };
result.Name = "Test";
return result;
}
Demo and DemoList look like this:
public interface INamedEnumerable<out T> : IEnumerable<T>
{
string Name { get; set; }
}
public class Demo
{
public int X { get { return 3; } }
public int Y { get; set; }
}
public class DemoList<T> : List<T>, INamedEnumerable<T>
{
public DemoList()
{
}
public string Name { get; set; }
}
I then cheked the ouput with fiddler
GET http://localhost:8086/api/Demo
and got the following:
XML (Accept header set to application/xml)
<ArrayOfDemo xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/XXX.WebAPI"><Demo><Y>2</Y></Demo><Demo><Y>0</Y></Demo><Demo><Y>1</Y></Demo></ArrayOfDemo>
JSON (Accept header set to application/json)
[{"X":3,"Y":2},{"X":3,"Y":0},{"X":3,"Y":1}]
My question is quite simple: Why is the X variable not serialized with the XML version (I thought that readonly properties were serialized) and more important, why in both cases is the Name property (which is writable) not serialized??
What are the alternatives to make this work like I expected?
Edit:
Please, note that I'm in a WebAPI context! By default, the XmlSerializer is automatically set to XmlMediaTypeFormatter and the JSONSerializer to JsonMediaTypeFormatter
This seems to be a bug... using the following workaround made the trick:
public class ListWrapper<T>
{
public ListWrapper(INamedEnumerable<T> list)
{
List = new List<T>(list);
Name = list.Name;
}
public List<T> List { get; set; }
public string Name { get; set; }
}
XML serializers only allows serialization of properties with "set" provided.
What are you using to serialize it? If you don't need attributes you could use DataContractSerializer as mentioned here. By default properties without a set are not serialized however using DataContractSerializer or implementing IXmlSerializable should do the trick for you.
using System;
using System.Runtime.Serialization;
using System.Xml;
[DataContract]
class MyObject {
public MyObject(Guid id) { this.id = id; }
[DataMember(Name="Id")]
private Guid id;
public Guid Id { get {return id;}}
}
static class Program {
static void Main() {
var ser = new DataContractSerializer(typeof(MyObject));
var obj = new MyObject(Guid.NewGuid());
using(XmlWriter xw = XmlWriter.Create(Console.Out)) {
ser.WriteObject(xw, obj);
}
}
}

Deserialization of xml file by using XmlArray?

I am trying to deserialize this xml structure.
<?xml version="1.0"?>
<DietPlan>
<Health>
<Fruit>Test</Fruit>
<Fruit>Test</Fruit>
<Veggie>Test</Veggie>
<Veggie>Test</Veggie>
</Health>
</DietPlan>
And I tried:
[Serializable]
[XmlRoot(ElementName = "DietPlan")]
public class TestSerialization
{
[XmlArray("Health")]
[XmlArrayItem("Fruit")]
public string[] Fruits { get; set; }
[XmlArray("Health")]
[XmlArrayItem("Veggie")]
public string[] Veggie { get; set; }
}
But this throws an exception "The XML element is already present in the current scope. Use XML attributes to specify another XML name or namespace for the element."
Thanks in adv.
You need a common type to be able to deserialize your XML, and with that you can define with the [XmlElement] namespace what type to instantiate depending on the name of the element, as shown below.
public class StackOverflow_15907357
{
const string XML = #"<?xml version=""1.0""?>
<DietPlan>
<Health>
<Fruit>Test</Fruit>
<Fruit>Test</Fruit>
<Veggie>Test</Veggie>
<Veggie>Test</Veggie>
</Health>
</DietPlan>";
[XmlRoot(ElementName = "DietPlan")]
public class TestSerialization
{
[XmlArray("Health")]
[XmlArrayItem("Fruit", Type = typeof(Fruit))]
[XmlArrayItem("Veggie", Type = typeof(Veggie))]
public Food[] Foods { get; set; }
}
[XmlInclude(typeof(Fruit))]
[XmlInclude(typeof(Veggie))]
public class Food
{
[XmlText]
public string Text { get; set; }
}
public class Fruit : Food { }
public class Veggie : Food { }
public static void Test()
{
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(XML));
XmlSerializer xs = new XmlSerializer(typeof(TestSerialization));
TestSerialization obj = (TestSerialization)xs.Deserialize(ms);
foreach (var food in obj.Foods)
{
Console.WriteLine("{0}: {1}", food.GetType().Name, food.Text);
}
}
}

Categories

Resources