如何在windows10通用应用程序中使用由xsd.exe生成的文件

本文关键字:exe xsd 文件 windows10 应用程序 | 更新日期: 2023-09-27 18:27:38

我使用xsd.exe从.xsd文件生成.cs文件。但当我将这些文件添加到windows 10通用空白应用程序时,我收到了System.SerializableAttribute()和System.ComponentModel.DesignerCategoryAttribute"缺少程序集引用"的错误(‌​"‌​code")。我用@t.ouvre的技巧解决了这个问题。然后,代码的任何一行都没有错误,但当我构建代码时,我收到一个错误,说"在模块System.dll中找不到类型System.ComponentModel.MarshalByValueComponent",而且它没有指定错误的确切位置。如何在windows 10通用应用程序中使用xsd.exe生成的文件?我需要对文件做什么才能使用它进行序列化和反序列化(在UWP中使用DataContractSerializer)

/// <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;
        }
    }
}

如何在windows10通用应用程序中使用由xsd.exe生成的文件

您可以伪造丢失的类(System.SerializableAttribute(),System.ComponentModel.DesignerCategoryAttribute),只需添加具有以下类定义的新文件:

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

对于序列化和反序列化,必须使用System.Runtime.serialization.DataContractSerializer。例如:

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");
            }
        }
    }
}

SerializableAttribute()和DesignerCategoryAttribute在uwp应用程序中不受支持。

要生成一个可以直接复制到UWP ap中的成功类,请使用以下提示:

这是在UWP应用程序中完成的,所以你可以继续做下去。XML序列化确实有一些限制,即必须有一个没有任何参数的默认构造函数。

如果你计划序列化一个没有构造函数的类,微软鼓励你在这种用例中使用DataContractSerializer。

现在代码很简单

  1. 首先实例化要序列化的obj
  2. 生成一个新的XmlSerializer对象
  3. 然后是XmlWriter obj,因为它接受了许多不同的编写器类,并且u需要实例化一个,我选择了字符串生成器进行演示
  4. 然后,只需在传入obj的序列化程序obj上调用serialize,xml编写器和xml编写器就会在字符串生成器obj中生成操作
  5. 然后我把它变成一根绳子。。从这里你可以用xml做任何事情。。要么保存它,要么玩它
  6. 最后一个toUpper方法刚刚添加,因为我需要一行调试点。。根本没有必要。。。

              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; }
                    }
    

序列化类的重用

  <?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:通过这种方法,您可以首先将所有序列化的类复制到应用程序中,并在必要时在应用程序内对它们进行反序列化。

现在,您所需要做的就是将xml复制到新的应用程序中,并使用我上面展示的相同技术实现去rilization。