0

I'm working on a Xamarin C# solution where I need to store data in xml format. In the first time I thought about writing manually a C# to XML and XML to C# method but my object has more than 100 attributes.

So I was wondering about using .NET DataContractSerializer to store them, while keeping references between the object. This seem like the best solution but I was wondering about the compatibility of this xml file with other languages.

If I want for example to parse it from JS or Java, will the XML parser in these languages be able to read it ? Since the way of storing is different I'm pretty sure it will not work natively. Will I be obliged again to write a custom deserialization method or is there a better way in C# to keep compatibility ?

Feuillebug
  • 41
  • 3
  • Why are you asking this question? XML is XML. You can deserialize it in any language. Is there *another* question behind this perhaps? Why do you think that XML produced by .NET has incompatibility issues? – Panagiotis Kanavos Apr 24 '19 at 07:47
  • DataContractSerializer adds customs attributes to be aware of references, I will be able to parse it manually but Deserializers will give me some empty object in some point. For example in the accepted answer : "https://stackoverflow.com/questions/1617528/net-xml-serialization-storing-reference-instead-of-object-copy" the Friend markup only store z:id. DataContract will understand, but others will give me an empty friend just with an id. – Feuillebug Apr 24 '19 at 07:50
  • No it doesn't. XML is XML. There are no references in XML. That answer shows some *explicit* settings that produce XML in a specific shape. If the serializer was configured to use a different XSD, it wouldn't produce these. It's still XML, it can still be parsed by any language out there. And since DataContractSerializer is used for SOAP web services, you *know* it's compatible – Panagiotis Kanavos Apr 24 '19 at 07:53
  • I think you want to parse the XML and save to Database. Then you should use a database that other languages can read besides c#. DataContractSerializer is the c# method for reading/writing a database. It is the database that need to be compatible to other languages. – jdweng Apr 24 '19 at 09:31
  • @jdweng [DataContractSerializer](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.serialization.datacontractserializer?view=netframework-4.8) has nothing to do with databases. It's the XML serializer used by WCF. The `DataContract` in the name refers to the WCF data contract. There's no compatibility issue as long as the XML string is well formed. Compatibility issues between schemas, yes. Languages, no, as long as they have at least one good XML parser – Panagiotis Kanavos Apr 24 '19 at 10:02
  • @ Panagiotis Kanavos : A Database is a type of Server. So DataContract is an interface between client and server to exchange data and can be used to transfer data to a database!!! – jdweng Apr 24 '19 at 10:24
  • @jdweng did you check the link? It's just an XML serializer. It doesn't communicate with anything, nor does it interface with anything. That's the job of WCF itself, or ADO.NET, or EF. DataContractSerializer is nothing more than an XML Serializer – Panagiotis Kanavos Apr 24 '19 at 12:35
  • As you said Panagiotis Kanavos, it's only for plain text writing, no database is involved in this problem. I think you have a good point in saying it's standard, the only point that was bothering me was about the added attribute to ensure the references. As you said, the xsd might solve the issue. I'll try your suggestion in a real example. – Feuillebug Apr 24 '19 at 12:43
  • @ Panagiotis Kanavos : Read my responses carefully. I am recommending a solution and not specifically saying specifically what DataContractSerializer is doing. My answers are technically correct. – jdweng Apr 24 '19 at 12:54

0 Answers0