Wie bekomme ich jede Eigenschaft als XElement?Ausgabe jeder Eigenschaft als XElement
Ich versuche im Grunde, ein IEnumerable-Objekt in XML in einem Webdienst zu konvertieren.
Hier ist mein Code Ich versuche, in XML zu konvertieren.
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();
}
bearbeiten
Kategorie Class Definition (mit EntityFramework 6-Code Erste)
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; }
Edit 2
Discount Klasse 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 Class Definition
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; }
}
Können Sie Beispiel mit XML hinzufügen? Und wer sollte das Endergebnis (die Liste der XElement) halten? Katzen? – KernelMode
Es muss eine XML-Zeichenfolge mit allen Kategorien und Eigenschaften erstellt werden. – Orion