我如何使用Windows 10通用应用程序中的xsd.exe生成的文件

我使用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)

///  [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; ///  [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] public usertype user { get { return this.userField; } set { this.userField = value; } } ///  [System.Xml.Serialization.XmlAttributeAttribute()] public string version { get { return this.versionField; } set { this.versionField = value; } } } ///  [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; ///  [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] public string email { get { return this.emailField; } set { this.emailField = value; } } ///  [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] public string password { get { return this.passwordField; } set { this.passwordField = value; } } } ///  [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; ///  [System.Xml.Serialization.XmlElementAttribute("request")] public request[] Items { get { return this.itemsField; } set { this.itemsField = value; } } } 

你可以伪造缺少的类(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"); } } } } 

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

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

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

如果您计划序列化一个没有构造函数的类,那么微软鼓励将DataContractSerializer用于此类用例。

现在代码非常简单

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

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

序列化类的reult

    Sheep 4  

更新:通过此方法,您可以先将所有序列化类复制到应用程序中,并在必要时在应用程序内反序列化它们。

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