如何在Windows10通用应用程序中使用xsd.exe生成的文件我使用xsd.exe从..cs文件。但是当我将文件添加到Windows10通用空白应用程序时,我收到错误消息“MissingassemblyreferencesforSystem.SerializableAttribute()andSystem.ComponentModel.DesignerCategoryAttribute("code")。我通过@t.ouvre的技巧解决了问题。然后在任何特定的代码行中都没有错误,但是当我构建代码时,我收到一条错误消息“无法在模块System.dll中找到System.ComponentModel.MarshalByValueComponent类型”并且它没有准确指定错误是。如何在windows10通用应用程序中使用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)]publicpartialclassrequest{privateusertypeuserField;私有字符串版本字段;///[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]publicusertypeuser{get{returnthis.userField;}设置{this.userField=value;}}///[System.Xml.Serialization.XmlAttributeAttribute()]publicstringversion{get{returnthis.versionField;}设置{this.versionField=value;}}}///[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd","4.6.81.0")][System.SerializableAttribute()][System.Diagnostics.DebuggerStepThroughAttribute()][System.ComponentModel.DesignerCategoryAttribute("code")]publicpartialclassusertype{privatestringemailField;私有字符串密码字段;///[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]publicstringemail{get{returnthis.emailField;}设置{this.emailField=value;}}///[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]publicstringpassword{get{returnthis.passwordField;}set{this.passwordField=value;}}}///[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd","4.6.81.0")][System.SerializableAttribute()][System.Diagnostics.DebuggerStepThroughAttribute()][System.ComponentModel.DesignerCategoryAttribute("代码”)][System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)][System.Xml.Serialization.XmlRootAttribute(名称pace="",IsNullable=false)]publicpartialclassNewDataSet{privaterequest[]itemsField;///[System.Xml.Serialization.XmlElementAttribute("request")]publicrequest[]Items{get{returnthis.itemsField;}设置{this.itemsField=value;您可以伪造缺失的类(System.SerializableAttribute()、System.ComponentModel.DesignerCategoryAttribute),只需添加一个包含这些类定义的新文件:namespaceSystem{internalclassSerializableAttribute:Attribute{}}DesignerCategoryAttribute:Attribute{publicDesignerCategoryAttribute(string_){}}}对于序列化和反序列化,您必须使用System.Runtime.Serialization.DataContractSerializer例如:DataContractSerializerserializer=newDataContractSerializer(typeof(request));varr=新请求{version="test"};使用(MemoryStreamms=newMemoryStream()){serializer.WriteObject(ms,r);ms.Seek(0,SeekOrigin.Begin);使用(varsr=newStreamReader(ms)){stringxmlContent=sr.ReadToEnd();Debug.WriteLine(xmlContent);ms.Seek(0,SeekOrigin.Begin);使用(XmlReaderreader=XmlReader.Create(sr)){vardeserialized=serializer.ReadObject(reader)作为请求;如果(反序列化!=null&&deserialized.version==r.version){Debug.WriteLine(“确定”);}}}}SerializableAttribute()和DesignerCategoryAttribute在uwp应用程序中不受支持。要生成可直接复制到UWP应用程序中的成功类,请使用以下提示:这是在UWP应用程序中完成的,因此您可以继续这样做。嗯,XML序列化确实有一些限制,您必须有一个不带任何参数的默认构造函数。如果您计划在没有构造函数的情况下序列化一个类,那么Microsoft鼓励在此类用例中使用DataContractSerializer。现在代码很简单首先实例化obj进行序列化。创建一个新的XmlSerializer对象。然后是XmlWriter对象,因为它接受许多不同的编写器类,您需要实例化一个字符串生成器,我选择它用于演示目的。然后只需在传递您的obj的序列化程序上调用序列化程序obj,xmlwriter和xmlwriter将在字符串生成器obj中生成op。然后我把它变成一个字符串..从这里你可以用xml做任何事情..保存或玩它...刚刚添加了最后一个toUpper方法bcoz我需要一行调试点..它根本没有必要。..privatevoidButton_Click(objectsender,RoutedEventArgse){Animala=newAnimal("Sheep",4);XmlSerializerm=newXmlSerializer(typeof(Animal));StringBuilderop=newStringBuilder();varx=XmlWriter.Create(op);m.Serialize(x,a);字符串s=op.ToString();varp=s.ToUpper();}publicclassAnimal{publicAnimal(stringname,intlegcount){this.name=name;这个.legcount=legcount;}publicAnimal(){this.name="default";this.legcount=10000000;}公共字符串名称{get;放;}publicintlegcount{得到;放;Sheep4的序列化类Update的结果:使用这种方法,您可以首先将所有序列化类复制到应用程序中,并在必要时在应用程序内反序列化它们。现在您需要做的就是将您的xml复制到新的应用程序中,并使用我在上面展示的相同技术来实现剥离。以上就是C#学习教程的全部内容:如何在Windows10通用应用程序中使用xsd.exe生成的文件。网络收藏不代表立场,如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
