5

I am generating .cs files from .xsd files using xsd.exe. But when I am adding the files to windows 10 universal blank app, I was getting error as "missing an assembly reference" for System.SerializableAttribute() and System.ComponentModel.DesignerCategoryAttribute(‌​"‌​code"). I fixed this by @t.ouvre's trick. Then there were no errors in any of the particular line of the code but when i am building the code i am getting an error saying that " Cannot find type System.ComponentModel.MarshalByValueComponent in module System.dll" and it doesn't specify exactly where the error is. How can I use the file generated by xsd.exe in windows 10 universal app? What are all the things that I need to do with the file 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;
        }
    }
}
Fresher
  • 493
  • 6
  • 18
  • can you post a generated file please ? – t.ouvre Dec 22 '15 at 09:06
  • System.SerializableAttribute(),System.ComponentModel.DesignerCategoryAttribute("code") are showing error as "does not exist,missing an assembly reference". It doesnt show any error in console application. – Fresher Dec 22 '15 at 10:12
  • It is possible to remove manually non compiling attributes ? (System.SerializableAttribute(),System.ComponentModel.DesignerCategoryAttribute("‌​code")) – t.ouvre Dec 22 '15 at 10:30
  • I have so many files. It will take so long to edit manually. but if no other option, I will have to do that. After removing it manually, will we need to add DataContract to get it works during serialization ? – Fresher Dec 22 '15 at 12:41
  • sorry for the delay, see my response ;) – t.ouvre Dec 23 '15 at 14:07

2 Answers2

6

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

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

for serialization and deserialization, you have to use System.Runtime.Serialization.DataContractSerializer. For example :

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");
            }
        }
    }
}
t.ouvre
  • 2,856
  • 1
  • 11
  • 17
  • Sorry. I have one more issue. The following error comes while building the code: Xaml Internal Error error WMC9999: Cannot find type System.ComponentModel.MarshalByValueComponent in module System.dll. – Fresher Dec 24 '15 at 07:20
  • Can you post some codes related to this error (or a link) ? I think this last error is not related to previous so I need more informations – t.ouvre Dec 24 '15 at 08:41
  • I just added the code which is in my question to the UWP. There are only two files in my project(App.xaml.cs,myfile.cs(contains code in the question)). I simply skipped the previous errors by the idea that you have given me. Then I had no errors on my code. But while compiling the code, I was getting "Cannot find type System.ComponentModel.MarshalByValueComponent in module System.dll". – Fresher Dec 24 '15 at 09:02
  • It shows only one error : "Cannot find type System.ComponentModel.MarshalByValueComponent in module System.dll" – Fresher Dec 24 '15 at 10:14
  • I meant the window named "Output", not the window named "Error List". the Ouput window gives more details. – t.ouvre Dec 24 '15 at 10:28
  • Same thing only, As I mentioned earlier : C:\Program Files (x86)\MSBuild\Microsoft\WindowsXaml\v14.0\8.2\Microsoft.Windows.UI.Xaml.Common.targets(350,5): Xaml Internal Error error WMC9999: Cannot find type System.ComponentModel.MarshalByValueComponent in module System.dll. – Fresher Dec 24 '15 at 10:34
  • ok. how did you create the project ? I found a problem related to Project Server CSOM. It's your case ? – t.ouvre Dec 24 '15 at 11:15
  • I freshly opened a new project. I simply added the code to fake missing classes. Its working perfectly. Thank you so much :) – Fresher Dec 28 '15 at 11:45
0

SerializableAttribute() and DesignerCategoryAttribute aren't supported in the uwp apps.

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

This is done in a UWP app so u can just go ahead and do this. Well the XML serialization does have some limitations that is you must have a default constructor without any parameters.

if you are planing to serialize a class that doesn't have a constructor microsoft encourages to use DataContractSerializer for such use cases.

Now the code is quite simple

  1. First instantiate the obj to serialize.
  2. Make a new XmlSerializer object.
  3. Then the XmlWriter obj , since it takes in many different writer classes and u Need to instantiate one i've chosen string builder for demo purposes.
  4. Then just simply call serialize on the serializer obj passing in your obj and the xmlwriter and the xml writer produces the op in the string builder obj.
  5. Then i just turn it into a string.. from here u can do anything with the xml .. save it or play with it ..
  6. the last toUpper method was just added bcoz i needed a line for debug point .. it's not necessary 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; }
                    }
    

The reult of the Serialized class

  <?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: By this method you can first copy all of your serialized classes into the app and deserialize them when necessary inside the app.

Now all you need to do is copy your xml into ur new app and implement deserilization using the same techniques i've shown above.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
Akash Gutha
  • 601
  • 8
  • 21