将此XML反序列化为对象的最佳方法

在其他示例中,我看到它与我的相似,有一个根节点,然后是一个数组节点,然后是一堆数组项。 我的问题是,我的根节点我的数组节点,所以我看到的示例似乎对我不起作用,我无法更改XML模式。 这是XML:

 
7f6da9df-1a91-4e20-8b66-07ac7548dc47 1 deal_abstract Abu Dhabi's IPIC Eyes Bond Sale After Cepsa Buy Abu Dhabi's IPIC has appointed banks for a potential sterling and euro-denominated bond issue, a document showed on Wednesday, after the firm acquired Spain's Cepsa in a $5 billion deal earlier this month... 02/24/2011 00:00:00 AM
1c3e57a0-c471-425a-87dd-051e69ecb7c5 2 deal_abstract Big Law Abuzz Over New China Security Review China's newly established foreign investment M&A review committee has been the subject of much legal chatter in the Middle Kingdom and beyond. Earlier this month, the State Council unveiled legislative guidance on… 02/23/2011 00:00:00 AM

这是我的class级:

 public class CurrentsResultsList { public Article[] Articles; } public class Article { public string Guid { get; set; } public int Order { get; set; } public string Type { get; set; } public string Title { get; set; } public string Summary { get; set; } public DateTime ArticleDate { get; set; } } 

这是来自外部API的XML响应。

你必须使用一些Xml属性,这个代码应该有希望产生你喜欢的xml,希望它有所帮助:

 using System; using System.IO; using System.Xml.Serialization; namespace xmlTest { class Program { static void Main(string[] args) { var articles = new Articles(); articles.ArticleArray = new ArticlesArticle[2] { new ArticlesArticle() { Guid = Guid.NewGuid(), Order = 1, Type = "deal_abstract", Title = "Abu Dhabi...", Summary = "Abu Dhabi...", ArticleDate = new DateTime(2011,2,24) }, new ArticlesArticle() { Guid = Guid.NewGuid(), Order = 2, Type = "deal_abstract", Title = "Abu Dhabi...", Summary = "China...", ArticleDate = new DateTime(2011,2,23) }, }; var sw = new StringWriter(); var xmlSer = new XmlSerializer(typeof (Articles)); var noNamespaces = new XmlSerializerNamespaces(); noNamespaces.Add("", ""); xmlSer.Serialize(sw, articles,noNamespaces); Console.WriteLine(sw.ToString()); } } [XmlRoot(ElementName = "articles", Namespace = "", IsNullable = false)] public class Articles { [XmlElement("article")] public ArticlesArticle[] ArticleArray { get; set; } } public class ArticlesArticle { [XmlElement("guid")] public Guid Guid { get; set; } [XmlElement("order")] public int Order { get; set; } [XmlElement("type")] public string Type { get; set; } [XmlElement("textType")] public string TextType { get; set; } [XmlElement("id")] public int Id { get; set; } [XmlElement("title")] public string Title { get; set; } [XmlElement("summary")] public string Summary { get; set; } [XmlElement("readmore")] public string Readmore { get; set; } [XmlElement("fileName")] public string FileName { get; set; } [XmlElement("articleDate")] public DateTime ArticleDate { get; set; } [XmlElement("articleDateType")] public string ArticleDateType { get; set; } } } 
  1. 把它放在视觉工作室里面的xml中
  2. 创建xsd架构
  3. 使用“C:\ Program Files \ Microsoft Visual Studio 8 \ SDK \ v2.0 \ Bin \ xsd.exe”“MyXsd.xsd”/ t:lib / l:cs / c /namespace:my.xsd / outputdir:“ C:\ testtttt”

现在你准备好了你的c#课程

现在你可以使用这个:

 internal class ParseXML { public static xsdClass ToClass(XElement ResponseXML) { return deserialize(ResponseXML.ToString(SaveOptions.DisableFormatting)); } private static result deserialize(string XML) { using (TextReader textReader = new StringReader(XML)) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(result)); return (result) xmlSerializer.Deserialize(textReader); } } } 

可能我能想到的最简单的方法是使用xsd工具。 你给它XML,它将从中生成一个模式。 您可能需要稍微调整一下架构,但它应该很接近。

从那里,您可以通过xsd发回相同的模式以从中生成类。

 >xsd test.xml Microsoft (R) Xml Schemas/DataTypes support utility [Microsoft (R) .NET Framework, Version 4.0.30319.1] Copyright (C) Microsoft Corporation. All rights reserved. Writing file 'test.xsd'. >xsd /c test.xsd Microsoft (R) Xml Schemas/DataTypes support utility [Microsoft (R) .NET Framework, Version 4.0.30319.1] Copyright (C) Microsoft Corporation. All rights reserved. Writing file 'test.cs'. 

结果:

 //------------------------------------------------------------------------------ //  // This code was generated by a tool. // Runtime Version:4.0.30319.1 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. //  //------------------------------------------------------------------------------ using System.Xml.Serialization; // // This source code was auto-generated by 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)] public partial class articles { private articlesArticle[] itemsField; ///  [System.Xml.Serialization.XmlElementAttribute("article", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public articlesArticle[] Items { get { return this.itemsField; } set { 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)] public partial class articlesArticle { private string guidField; private string orderField; private string typeField; private string textTypeField; private string idField; private string titleField; private string summaryField; private string readmoreField; private string fileNameField; private string articleDateField; private string articleDateTypeField; ///  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string guid { get { return this.guidField; } set { this.guidField = value; } } ///  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string order { get { return this.orderField; } set { this.orderField = value; } } ///  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string type { get { return this.typeField; } set { this.typeField = value; } } ///  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string textType { get { return this.textTypeField; } set { this.textTypeField = value; } } ///  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string id { get { return this.idField; } set { this.idField = value; } } ///  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string title { get { return this.titleField; } set { this.titleField = value; } } ///  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string summary { get { return this.summaryField; } set { this.summaryField = value; } } ///  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string readmore { get { return this.readmoreField; } set { this.readmoreField = value; } } ///  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string fileName { get { return this.fileNameField; } set { this.fileNameField = value; } } ///  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string articleDate { get { return this.articleDateField; } set { this.articleDateField = value; } } ///  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string articleDateType { get { return this.articleDateTypeField; } set { this.articleDateTypeField = value; } } }