如何将每个属性都作为XElement?输出每个属性为XElement
我基本上试图将IEnumerable对象转换为Web服务中的XML。
这是我想要转换为XML的代码。
using (var db = new nopMass())
{
var cats = db.Categories
.Where(x => x.Deleted == false
&& x.Published == true)
.OrderBy(x => x.DisplayOrder)
.AsEnumerable()
.Select(cat => new Category
{
Id = cat.Id,
Name = cat.Name,
Description = cat.Description,
MetaKeywords = cat.MetaKeywords,
MetaDescription = cat.MetaDescription,
MetaTitle = cat.MetaTitle,
PictureId = cat.PictureId,
DisplayOrder = cat.DisplayOrder,
CreatedOnUtc = cat.CreatedOnUtc,
Product_Category_Mapping = cat.Product_Category_Mapping,
ParentCategoryId = cat.ParentCategoryId,
})
.ToArray();
XElement Configuration = new XElement("Collection",
cats
.ToList()
.Select(c => new XElement("Element", c)));
return Configuration.ToString();
}
编辑
类别分类定义(使用的EntityFramework 6代码优先)
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
[Table("Category")]
public partial class Category
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Category()
{
Product_Category_Mapping = new HashSet<Product_Category_Mapping>();
Discounts = new HashSet<Discount>();
}
public int Id { get; set; }
[Required]
[StringLength(400)]
public string Name { get; set; }
public string Description { get; set; }
public int CategoryTemplateId { get; set; }
[StringLength(400)]
public string MetaKeywords { get; set; }
public string MetaDescription { get; set; }
[StringLength(400)]
public string MetaTitle { get; set; }
public int ParentCategoryId { get; set; }
public int PictureId { get; set; }
public int PageSize { get; set; }
public bool AllowCustomersToSelectPageSize { get; set; }
[StringLength(200)]
public string PageSizeOptions { get; set; }
[StringLength(400)]
public string PriceRanges { get; set; }
public bool ShowOnHomePage { get; set; }
public bool IncludeInTopMenu { get; set; }
public bool HasDiscountsApplied { get; set; }
public bool SubjectToAcl { get; set; }
public bool LimitedToStores { get; set; }
public bool Published { get; set; }
public bool Deleted { get; set; }
public int DisplayOrder { get; set; }
public DateTime CreatedOnUtc { get; set; }
public DateTime UpdatedOnUtc { get; set; }
编辑2
折扣类Difinition
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
[Table("Discount")]
public partial class Discount
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Discount()
{
DiscountRequirements = new HashSet<DiscountRequirement>();
DiscountUsageHistories = new HashSet<DiscountUsageHistory>();
Categories = new HashSet<Category>();
Products = new HashSet<Product>();
}
public int Id { get; set; }
[Required]
[StringLength(200)]
public string Name { get; set; }
public int DiscountTypeId { get; set; }
public bool UsePercentage { get; set; }
public decimal DiscountPercentage { get; set; }
public decimal DiscountAmount { get; set; }
public DateTime? StartDateUtc { get; set; }
public DateTime? EndDateUtc { get; set; }
public bool RequiresCouponCode { get; set; }
[StringLength(100)]
public string CouponCode { get; set; }
public int DiscountLimitationId { get; set; }
public int LimitationTimes { get; set; }
public int? MaximumDiscountedQuantity { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<DiscountRequirement> DiscountRequirements { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<DiscountUsageHistory> DiscountUsageHistories { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Category> Categories { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Product> Products { get; set; }
}
Product_Category_Mapping类定义
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
public partial class Product_Category_Mapping
{
public int Id { get; set; }
public int ProductId { get; set; }
public int CategoryId { get; set; }
public bool IsFeaturedProduct { get; set; }
public int DisplayOrder { get; set; }
public virtual Category Category { get; set; }
public virtual Product Product { get; set; }
}
您可以添加XML示例吗?谁应该持有最终结果(XElement名单)?猫? – KernelMode
它需要创建一个包含所有类别和属性的XML字符串。 – Orion