How can I use the file generated by xsd.exe in a universal Windows 10 application

I am generating .cs files from .xsd files using xsd.exe. But when I add files to the universal empty Windows 10 application, I received an error as a "missing assembly reference" for System.SerializableAttribute () and System.ComponentModel.DesignerCategoryAttribute ("code"). I fixed it with @ t.ouvre trick. Then there were no errors in any particular line of code, but when I create the code, I get the error message "I can not find the type System.ComponentModel.MarshalByValueComponent in the System.dll module", and it does not exactly indicate where the error is. How can I use the file created by xsd.exe in a universal Windows 10 application? What do I need to do with the file in order to use it for serialization and deserialization (using DataContractSerializer in UWP)

/// <remarks/> [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.0")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)] public partial class request { private usertype userField; private string versionField; /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] public usertype user { get { return this.userField; } set { this.userField = value; } } /// <remarks/> [System.Xml.Serialization.XmlAttributeAttribute()] public string version { get { return this.versionField; } set { this.versionField = value; } } } /// <remarks/> [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.0")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] public partial class usertype { private string emailField; private string passwordField; /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] public string email { get { return this.emailField; } set { this.emailField = value; } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] public string password { get { return this.passwordField; } set { this.passwordField = value; } } } /// <remarks/> [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.0")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)] public partial class NewDataSet { private request[] itemsField; /// <remarks/> [System.Xml.Serialization.XmlElementAttribute("request")] public request[] Items { get { return this.itemsField; } set { this.itemsField = value; } } } 
+5
source share
2 answers

You can fake missing classes (System.SerializableAttribute (), System.ComponentModel.DesignerCategoryAttribute), just add new files with abstract class definitions:

 namespace System { internal class SerializableAttribute : Attribute { } } namespace System.ComponentModel { internal class DesignerCategoryAttribute : Attribute { public DesignerCategoryAttribute(string _) { } } } 

for serialization and deserialization you should use System.Runtime.Serialization.DataContractSerializer. For instance:

 DataContractSerializer serializer = new DataContractSerializer(typeof(request)); var r = new request { version = "test" }; using (MemoryStream ms = new MemoryStream()) { serializer.WriteObject(ms, r); ms.Seek(0, SeekOrigin.Begin); using (var sr = new StreamReader(ms)) { string xmlContent = sr.ReadToEnd(); Debug.WriteLine(xmlContent); ms.Seek(0, SeekOrigin.Begin); using (XmlReader reader = XmlReader.Create(sr)) { var deserialized = serializer.ReadObject(reader) as request; if (deserialized != null && deserialized.version == r.version) { Debug.WriteLine("ok"); } } } } 
+6
source

SerializableAttribute () and DesignerCategoryAttribute are not supported in uwp applications.

To create a successful class that can be copied directly to UWP ap, use the following tip:

This is done in the UWP app, so you can just go ahead and do it. Well, XML serialization has some limitations that you should have a default constructor without any parameters.

if you plan to serialize a class that does not have a constructor, Microsoft recommends using the DataContractSerializer for such use cases.

Now the code is pretty simple

  • First create an obj instance for serialization.
  • Create a new XmlSerializer object.
  • Then XmlWriter obj, as it takes in many different writer classes and u It is necessary to create an instance of one of the selected line builders for demonstration purposes.
  • Then just just call serialization in the obj serializer passing in your object and xmlwriter, and xml-writer returns op in the obj line builder.
  • Then I just turn it into a string .. from here you can do something with xml .. save or play with it ..
  • The last toUpper method was just added by bcoz. I need a line for the debug point .. I don’t need it at all ...

      private void Button_Click( object sender , RoutedEventArgs e ) { Animal a = new Animal("Sheep" , 4); XmlSerializer m = new XmlSerializer(typeof(Animal)); StringBuilder op = new StringBuilder(); var x = XmlWriter.Create(op); m.Serialize(x , a); string s = op.ToString(); var p = s.ToUpper(); } public class Animal { public Animal( string name , int legcount ) { this.name = name; this.legcount = legcount; } public Animal() { this.name = "default"; this.legcount = 10000000; } public string name { get; set; } public int legcount { get; set; } } 

Serialized class result

  <?xml version="1.0" encoding="utf-16"?> <Animal xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <name>Sheep</name> <legcount>4</legcount> </Animal> 

UPDATE: with this method, you can first copy all your serialized classes into the application and deserialize them when necessary inside the application.

Now all you have to do is copy xml into a new application and implement deserialization using the same methods as shown above.

0
source

Source: https://habr.com/ru/post/1238921/


All Articles