当前位置: 首页 > 编程语言 > C#

C#LearningTutorial-BestwaytodeserializethisXMLintoanobject分享

时间:2023-04-10 23:28:24 C#

BestwaytodeserializethisXMLintoanobject项目。我的问题是,我的根节点是我的数组节点,所以我看到的示例似乎对我不起作用,而且我无法更改XML架构。这是XML:7f6da9df-1a91-4e20-8b66-07ac7548dc471deal_abstract阿布扎比的IPIC在Cepsa购买后关注债券销售阿布扎比的IPIC已指定银行发行潜在的和欧元计价的债券,周三的一份文件显示,在公司之后本月早些时候以50亿的价格收购了西班牙的Cepsa...02/24/201100:00:00AM1c3e57a0-c471-425a-87dd-051e69ecb7c52deal_abstract新中国安全审查引发大法律热议中国新成立的外商投资并购审查委员会一直是中国及其他地区法律界争论不休的话题。本月早些时候,国务院公布了立法指南……02/23/201100:00:00AM这是我的等级:publicclassCurrentsResultsList{publicArticle[]Articles;}publicclassArticle{publicstringGuid{get;放;}publicintOrder{得到;放;}公共字符串类型{得到;放;}公共字符串标题{得到;放;}公共字符串摘要{得到;放;}publicDateTimeArticleDate{get;放;}}这是来自外部API的XML响应您必须使用一些Xml属性,这段代码应该会产生您喜欢的xml,希望对您有所帮助:使用System.IO;使用System.Xml.Serialization;namespacexmlTest{classProgram{staticvoidMain(string[]args){vararticles=newArticles();articles.ArticleArray=newArticlesArticle[2]{newArticlesArticle(){Guid=Guid.NewGuid(),Order=1,Type="deal_abstract",Title="AbuDhabi...",Summary="AbuDhabi...",ArticleDate=newDateTime(2011,2,24)},newArticlesArticle(){Guid=Guid.NewGuid(),Order=2,Type="deal_abstract",Title="AbuDhabi...",Summary="中国...",ArticleDate=newDateTime(2011,2,23)},};varsw=newStringWriter();varxmlSer=newXmlSerializer(typeof(文章));varnoNamespaces=newXmlSerializerNamespaces();noNamespaces.Add("","");xmlSer.Serialize(sw,文章,noNamespaces);Console.WriteLine(sw.ToString());}}[XmlRoot(ElementName="articles",Namespace="",IsNullable=false)]publicclassArticles{[XmlElement("article")]公共ArticlesArticle[]ArticleArray{get;放;}}publicclassArticlesArticle{[XmlElement("guid")]publicGuidGuid{get;放;}[XmlElement("order")]publicintOrder{get;放;}[XmlElement("type")]publicstringType{get;放;}[XmlElement("textType")]publicstringTextType{get;放;}[XmlElement("id")]publicintId{get;放;}[XmlElement("title")]publicstringTitle{get;放;}[XmlElement("summary")]publicstringSummary{get;放;}[XmlElement("readmore")]publicstringReadmore{get;放;}[XmlElement("fileName")]publicstringFileName{get;放;}[XmlElement("articleDate")]publicDateTimeArticleDate{get;放;}[XmlElement("articleDateType")]publicstringArticleDateType{get;放;}}}将其放入VisualStudio中的xml中创建xsd架构使用“C:\ProgramFiles\MicrosoftVisualStudio8SDKv2.0Binxsd.exe”“MyXsd.xsd”/t:lib/l:cs/c/namespace:my.xsd/outputdir:"C:testtttt"现在你已经准备好你的c#类了,你可以使用这个:internalclassParseXML{publicstaticxsdClassToClass(XElementResponseXML){returndeserialize(ResponseXML.ToString(SaveOptions.DisableFormatting));}privatestaticresultdeserialize(stringXML){using(TextReadertextReader=newStringReader(XML)){XmlSerializerxmlSerializer=newXmlSerializer(typeof(result));返回(结果)xmlSerializer。反序列化(文本阅读器);可能我能想到的最简单的方法是使用一个xsd工具,你给它XML,它会从中生成一个模式。您可能需要稍微调整架构,但应该很接近。从那里,您可以通过xsd发回相同的模式以从中生成类。>xsdtest.xmlMicrosoft(R)Xml模式/数据类型支持实用程序[Microsoft(R).NETFramework,版本4.0.30319.1]版权所有(C)MicrosoftCorporation。版权所有。写入文件“test.xsd”。>xsd/ctest.xsdMicrosoft(R)Xml模式/数据类型支持实用程序[Microsoft(R).NETFramework,版本4.0.30319.1]版权所有(C)MicrosoftCorporation。版权所有。写入文件“test.cs”。结果:以上就是C#学习教程的全部内容:将这个XML反序列化为对象的最佳方式。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注——//——————————————------------------------------------------------------////此代码由工具生成。//运行时版本:4.0.30319.1////对此文件的更改可能会导致不正确的行为,并且如果//重新生成代码,将会丢失。////--------------------------------------------------------------------------使用System.Xml.Serialization;////此源代码由xsd自动生成,Version=4.0.30319.1。/////[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd","4.0.30319.1")][System.SerializableAttribute()][System.Diagnostics.DebuggerStepThroughAttribute()][System.ComponentModel.DesignerCategoryAttribute("code")][System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)][System.Xml.Serialization.XmlRootAttribute(Namespace="",IsNullable=false)]publicpartialclassarticles{privatearticlesArticle[]itemsField;///[System.Xml.Serialization.XmlElementAttribute("article",Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]publicarticlesArticle[]Items{get{returnthis.itemsField;}设置{this.itemsField=value;}}}///[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd","4.0.30319.1")][System.SerializableAttribute()][System.Diagnostics.DebuggerStepThroughAttribute()][System.ComponentModel.DesignerCategoryAttribute("code")][System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]publicpartialclassarticlesArticle{privatestringguid字段;私有字符串orderField;私有字符串类型字段;私有字符串文本类型字段;私有字符串idField;私有字符串标题字段;私有字符串摘要字段;私有字符串readmoreField;私有字符串文件名字段;私有字符串文章日期字段;私有字符串articleDateTypeField;///[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]publicstringguid{get{returnthis.guidField;}设置{this.guidField=value;}}///[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]publicstringorder{get{returnthis.orderField;}设置{this.orderField=value;}}///[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]publicstringtype{get{returnthis.typeField;}set{this.typeField=value;}}///[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Un合格的)]publicstringtextType{get{returnthis.textTypeField;}set{this.textTypeField=value;}}///[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]publicstringid{get{returnthis.idField;}设置{this.idField=value;}}///[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]publicstringtitle{get{returnthis.titleField;}设置{this.titleField=value;}}///[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]publicstringsummary{get{returnthis.summaryField;}设置{this.summaryField=value;}}///[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]publicstringreadmore{get{returnthis.readmoreField;}设置{this.readmoreField=value;}}///[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]publicstringfileName{get{returnthis.fileNameField;}set{this.fileNameField=value;}}///[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]publicstringarticleDate{get{returnthis.articleDateField;}设置{this.articleDateField=value;}}///[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]publicstringarticleDateType{get{returnthis.articleDateTypeField;}set{this.articleDateTypeField=value;}}}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: