提到question时,我曾问过有关XML反序列化的信息。
XML对象:
[XmlRoot(ElementName = "root")]
public class RootMIR
{
[XmlArray(ElementName = "MIRs")]
public List<MIR> MIRs { get; set; }
}
[XmlRoot(ElementName = "MIR")]
public class MIR
{
[XmlElement(ElementName = "issue_data")]
public IssueData IssueData { get; set; }
[XmlElement(ElementName = "reply_data")]
public ReplyData ReplyData { get; set; }
[XmlElement(ElementName = "receive_data")]
public ReceiveData ReceiveData { get; set; }
/*[XmlElement(ElementName = "items")]
public Items Items { get; set; }*/
[XmlArray(ElementName = "items")]
public List<Item> Items { get; set; }
[XmlElement(ElementName = "submission_data")]
public SubmissionData SubmissionData { get; set; }
[XmlIgnore]
public int ID { get; set; }
[XmlAttribute(AttributeName = "id")]
public string ID_text
{
get { return ID.ToString(); }
set { ID = Convert.ToInt32(value); }
}
[XmlAttribute(AttributeName = "number")]
public string _numberText;
[XmlIgnore]
public int? Number
{
get { return _numberText == "" ? (int?)null : Convert.ToInt32(_numberText); }
set { _numberText = value == null ? "" : value.ToString(); }
}
/*[XmlIgnore]
public int? Number
{
get { return Number_text == "" ? (int?)null : Convert.ToInt32(Number_text); }
set { Number_text = value.ToString(); }
}
[XmlAttribute(AttributeName = "number")]
public string Number_text { get; set; }*/
[XmlIgnore]
public int? Revision
{
get { return Revision_text == "" ? (int?)null : Convert.ToInt32(Revision_text); }
set { Revision_text = value == null ? "" : value.ToString(); }
}
[XmlAttribute(AttributeName = "revision")]
public string Revision_text { get; set; }
}
[XmlRoot(ElementName = "issue_data")]
public class IssueData
{
[XmlIgnore]
public DateTime? IssueDate { get; set; }
[XmlElement(ElementName = "issue_date")]
public string IssueDateText
{
get { return (IssueDate.HasValue ? IssueDate.ToString() : null); }
set { IssueDate = Convert.ToDateTime(value); }
}
[XmlElement(ElementName = "from")]
public string From { get; set; }
[XmlElement(ElementName = "to")]
public string To { get; set; }
[XmlElement(ElementName = "author")]
public string Author { get; set; }
[XmlElement(ElementName = "attn")]
public string Attn { get; set; }
[XmlElement(ElementName = "field")]
public string Field { get; set; }
[XmlElement(ElementName = "material_group")]
public string MaterialGroup { get; set; }
[XmlElement(ElementName = "related_sub")]
public string RelatedSub { get; set; }
}
[XmlRoot(ElementName = "reply_data")]
public class ReplyData
{
[XmlElement(ElementName = "reply_date")]
public string ReplyDate { get; set; }
[XmlElement(ElementName = "action_code")]
public string ActionCode { get; set; }
[XmlElement(ElementName = "reply_from")]
public string ReplyFrom { get; set; }
}
[XmlRoot(ElementName = "receive_data")]
public class ReceiveData
{
[XmlElement(ElementName = "receive_date")]
public string ReceiveDate { get; set; }
[XmlElement(ElementName = "receive_by")]
public string ReceiveBy { get; set; }
}
[XmlRoot(ElementName = "item")]
public class Item
{
[XmlElement(ElementName = "serial")]
public string Serial { get; set; }
[XmlElement(ElementName = "boq_code")]
public string BoqCode { get; set; }
[XmlElement(ElementName = "item_details")]
public string ItemDetails { get; set; }
[XmlElement(ElementName = "model")]
public string Model { get; set; }
[XmlElement(ElementName = "manufacturer")]
public string Manufacturer { get; set; }
[XmlElement(ElementName = "size")]
public string Size { get; set; }
[XmlElement(ElementName = "uom")]
public string UoM { get; set; }
[XmlElement(ElementName = "qty")]
public string Quantity { get; set; }
[XmlElement(ElementName = "approval")]
public string Approval { get; set; }
[XmlElement(ElementName = "approved_qty")]
public string ApprovedQuantity { get; set; }
[XmlElement(ElementName = "is_lumbsum")]
public string IsLumbsum { get; set; }
}
[XmlRoot(ElementName = "submission_data")]
public class SubmissionData
{
//[XmlElement(ElementName = "submitted")]
//public string Submitted { get; set; }
[XmlElement(ElementName = "status")]
public string Status { get; set; }
}
对于序列化器对象,
代码:
public static void ReloadDocumentFromDisk()
{
using (FileStream fileStream = new FileStream(BaseDir + FileName + Extension, FileMode.Open))
{
DocMIR = (RootMIR)serializer.Deserialize(fileStream);
}
}
问题是我收到List<Item>
的空白列表,但是List<MIR>
正常运行,我检查了很多遍代码,但没有发现我所缺少的是什么。
这是我添加断点以检查代码的结果:
MIR的数量为10,因为我的XML文件中有10个元素,但是每个MIR中的项目数为0
答案 0 :(得分:1)
您需要使用XmlArrayItem
属性来指示每个“项目”的元素名称都与您要反序列化的Item
的类名称不同。
[XmlArray(ElementName = "items")]
[XmlArrayItem(ElementName = "item")]
public List<Item> Items { get; set; }
如果您的Item
类被称为item
,那么在没有XmlArrayItem
的情况下可以正常工作。这就是为什么仅在使用MIR
属性时,您对MIRs
属性使用XmlArray
可以正常工作的原因,因为内部元素的类名完全匹配。
答案 1 :(得分:1)
日期时间不能为null。因此,您需要能够处理datetime为null的情况。所以我做了一个新的DateTime()。请参阅下面我用您的xml测试过的代码
注意:您在以前的帖子中回答了您一个问题,即如何使用Xml Linq使用匿名解析来解析xml。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Globalization;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
IFormatProvider provider = CultureInfo.InvariantCulture;
var results = doc.Descendants("MIR").Select(mir => new
{
Number = (string)mir.Attribute("number"),
Revision = (string)mir.Attribute("revision"),
From = (string)mir.Element("issue_data").Element("from"),
Material = (string)mir.Element("issue_data").Element("material_group"),
Field = (string)mir.Element("issue_data").Element("field"),
Submittal = (string)mir.Element("issue_data").Element("related_sub"),
To = (string)mir.Element("issue_data").Element("to"),
Atten = (string)mir.Element("issue_data").Element("attn"),
IssueDate = DateTime.ParseExact((string)mir.Descendants("issue_date").FirstOrDefault(), "dd-M-yyyy",provider),
ReplyDate = (string)mir.Descendants("receive_date").FirstOrDefault() == string.Empty ? new DateTime() : DateTime.ParseExact((string)mir.Descendants("receive_date").FirstOrDefault(), "dd-M-yyyy", provider),
ActionCode = (string)mir.Element("reply_data").Element("action_code"),
Author = (string)mir.Element("issue_data").Element("author"),
IsSubmitted = (string)mir.Element("submission_data").Element("submitted"),
Status = (string)mir.Element("submission_data").Element("status")
}).First();
}
}
}