Custom Entity And XElement Convertion

Custom Entity And XElement Convertion

Custom Entity to XElement

private XElement WorkItemToElement(Entity.Business.WorkItem item)
        {
            return new XElement("workItem",
                                new XElement("desc", item.Desc),
                                new XAttribute("id", item.Id),
                                new XAttribute("name", item.Name),
                                new XAttribute("workType", EnumHelper.GetEnumNameFrom(item.WorkType)),
                                new XAttribute("createDate", item.CreateDate),
                                new XAttribute("isFinished", item.IsFinished ? Boolean.TrueString : Boolean.FalseString));
        }


XElement To Custom Entity

   private Entity.Business.WorkItem ElementToWorkItem(XElement element)
        {
            WorkItemType workType;
            if (!Enum.TryParse(element.Attribute("workType").Value, out workType)) return null;

            var xElement = element.Element("desc");
            var strDesc = string.Empty;

            if (xElement != null)
                strDesc = xElement.Value;

            return new Entity.Business.WorkItem
                {
                    Id = new Guid(element.Attribute("id").Value),
                    Name = element.Attribute("name").Value,
                    CreateDate = DateTime.Parse(element.Attribute("createDate").Value),
                    Desc = strDesc,
                    WorkType = workType,
                    IsFinished = bool.Parse(element.Attribute("isFinished").Value)
                };
        }

 


你可能感兴趣的:(element)