C# 将每个属性输出为XElement

C# 将每个属性输出为XElement,c#,xml,C#,Xml,如何将每个属性作为XElement获取 我基本上是尝试在web服务中将IEnumerable对象转换为XML 这是我试图转换为XML的代码 using (var db = new nopMass()) { var cats = db.Categories .Where(x => x.Deleted == false

如何将每个属性作为XElement获取

我基本上是尝试在web服务中将IEnumerable对象转换为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代码)


所以您实际上需要xml字符串,最简单的方法是简单的xml序列化。例如,如果您有以下类别:

var cats = new List<Category> {
    new Category { Id = 1, Name = "Auto", Description = "blah-blah-blah" },
    new Category { Id = 2, Name = "Moto", Description = "bikes!" }
};
var cats=新列表{
新类别{Id=1,Name=“Auto”,Description=“blah blah blah”},
新类别{Id=2,Name=“Moto”,Description=“bikes!”}
};
序列化看起来像

var serializer = new XmlSerializer(typeof(List<Category>), 
                                   new XmlRootAttribute("Categories"));
using (var writer = new StringWriter())
{
    serializer.Serialize(writer, cats);
    return writer.ToString();
}
var serializer=新的XmlSerializer(typeof(List),
新的XmlRootAttribute(“类别”);
使用(var writer=new StringWriter())
{
序列化器。序列化(编写器,cats);
返回writer.ToString();
}
输出:

<?xml version="1.0" encoding="utf-16"?>
<Categories xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Category>
    <Id>1</Id>
    <Name>Auto</Name>
    <Description>blah-blah-blah</Description>
  </Category>
  <Category>
    <Id>2</Id>
    <Name>Moto</Name>
    <Description>bikes!</Description>
  </Category>
</Categories>

1.
汽车
废话废话
2.
摩托
自行车!
使用节点的属性。它将返回您想要的内容

 return Configuration.OuterXml;

这是我能想出的最好的解决办法。它是有效的,所以我会接受它作为我的答案

    // GETAll api/category
    [WebMethod]
    public string GetAllCategories()
    {
        using (var db = new nopMass())
        {
            var cats = db.Categories
                        .Where(x => x.ParentCategoryId == 1
                                    && x.Deleted == false
                                    && x.Published == true)
                        .OrderBy(c => c.ParentCategoryId)
                        .ThenBy(c => c.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,
                        })
                        .ToList();

            string XML = "";

            #region BuildXMLString

            XML += "<Collection>";
            foreach (var item in cats)
            {
                XML += "<Category>";

                XML += "<Id>";
                XML += item.Id.ToString();
                XML += "</Id>";

                XML += "<Name>";
                XML += item.Name;
                XML += "</Name>";

                XML += "<Description>";
                XML += item.Description;
                XML += "</Description>";

                XML += "<MetaKeywords>";
                XML += item.MetaKeywords;
                XML += "</MetaKeywords>";

                XML += "<MetaDescription>";
                XML += item.MetaDescription;
                XML += "</MetaDescription>";

                XML += "<MetaTitle>";
                XML += item.MetaTitle;
                XML += "</MetaTitle>";

                XML += "<PictureUrl>";
                try
                {
                    XML += GetPictureUrl(item.PictureId);
                }
                catch { }
                XML += "</PictureUrl>";

                XML += "<DisplayOrder>";
                XML += item.DisplayOrder.ToString();
                XML += "</DisplayOrder>";

                XML += "<CreatedOnUtc>";
                XML += item.CreatedOnUtc.ToString();
                XML += "</CreatedOnUtc>";

                XML += "<ParentCategoryId>";
                XML += item.ParentCategoryId.ToString();
                XML += "</ParentCategoryId>";

                XML += "</Category>";
            }
            XML += "</Collection>";

            #endregion

            return XML;

        }
    }
//获取所有api/类别
[网络方法]
公共字符串GetAllCategories()
{
使用(var db=new nopMass())
{
var cats=db.Categories
.其中(x=>x.ParentCategoryId==1
&&x.已删除==false
&&x.已发布==真)
.OrderBy(c=>c.ParentCategoryId)
.ThenBy(c=>c.DisplayOrder)
.可计算的()
.选择(类别=>新类别
{
Id=类别Id,
Name=cat.Name,
描述=类别描述,
MetaKeywords=cat.MetaKeywords,
MetaDescription=类别MetaDescription,
MetaTitle=类别MetaTitle,
PictureId=cat.PictureId,
DisplayOrder=类别DisplayOrder,
CreatedOnUtc=cat.CreatedOnUtc,
产品类别映射=类别产品类别映射,
ParentCategoryId=cat.ParentCategoryId,
})
.ToList();
字符串XML=”“;
#区域BuildXMLString
XML+=“”;
foreach(CAT中的var项目)
{
XML+=“”;
XML+=“”;
XML+=item.Id.ToString();
XML+=“”;
XML+=“”;
XML+=item.Name;
XML+=“”;
XML+=“”;
XML+=项目描述;
XML+=“”;
XML+=“”;
XML+=item.MetaKeywords;
XML+=“”;
XML+=“”;
XML+=item.MetaDescription;
XML+=“”;
XML+=“”;
XML+=item.MetaTitle;
XML+=“”;
XML+=“”;
尝试
{
XML+=GetPictureUrl(item.PictureId);
}
捕获{}
XML+=“”;
XML+=“”;
XML+=item.DisplayOrder.ToString();
XML+=“”;
XML+=“”;
XML+=item.CreatedOnUtc.ToString();
XML+=“”;
XML+=“”;
XML+=item.ParentCategoryId.ToString();
XML+=“”;
XML+=“”;
}
XML+=“”;
#端区
返回XML;
}
}

您能用XML添加示例吗?谁应该持有最终结果(XElement列表)?cats?它需要创建一个包含所有类别和属性的XML字符串。
Select(c=>new-XElement(“Element”,c))
将为您提供类的名称,而不是属性的值。op已将
配置
定义为
XElement
,因此,上面的内容应该可以工作。请考虑Xelementt的内容没有和OuterXml用于配置?我得到以下错误:“System.Web.Services.Protocols.SoapException:服务器无法处理请求。-->System.InvalidOperationException:反映类型“System.Collections.Generic.List”的错误这是从web服务调用的。@Orion能否将
类别
类定义添加到问题中?@Orion Product\u Category\u映射和折扣具有默认的无参数构造函数?问题已更新,以包括Product\u Category\u映射和折扣类。我也在使用我的web服务时尝试了您的代码,但出现了相同的错误。@Orion刚刚检查了您的代码-如果您将
ICollection
类型的导航属性标记为
[XmlIgnore]
属性,则序列化工作正常
<?xml version="1.0" encoding="utf-16"?>
<Categories xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Category>
    <Id>1</Id>
    <Name>Auto</Name>
    <Description>blah-blah-blah</Description>
  </Category>
  <Category>
    <Id>2</Id>
    <Name>Moto</Name>
    <Description>bikes!</Description>
  </Category>
</Categories>
 return Configuration.OuterXml;
    // GETAll api/category
    [WebMethod]
    public string GetAllCategories()
    {
        using (var db = new nopMass())
        {
            var cats = db.Categories
                        .Where(x => x.ParentCategoryId == 1
                                    && x.Deleted == false
                                    && x.Published == true)
                        .OrderBy(c => c.ParentCategoryId)
                        .ThenBy(c => c.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,
                        })
                        .ToList();

            string XML = "";

            #region BuildXMLString

            XML += "<Collection>";
            foreach (var item in cats)
            {
                XML += "<Category>";

                XML += "<Id>";
                XML += item.Id.ToString();
                XML += "</Id>";

                XML += "<Name>";
                XML += item.Name;
                XML += "</Name>";

                XML += "<Description>";
                XML += item.Description;
                XML += "</Description>";

                XML += "<MetaKeywords>";
                XML += item.MetaKeywords;
                XML += "</MetaKeywords>";

                XML += "<MetaDescription>";
                XML += item.MetaDescription;
                XML += "</MetaDescription>";

                XML += "<MetaTitle>";
                XML += item.MetaTitle;
                XML += "</MetaTitle>";

                XML += "<PictureUrl>";
                try
                {
                    XML += GetPictureUrl(item.PictureId);
                }
                catch { }
                XML += "</PictureUrl>";

                XML += "<DisplayOrder>";
                XML += item.DisplayOrder.ToString();
                XML += "</DisplayOrder>";

                XML += "<CreatedOnUtc>";
                XML += item.CreatedOnUtc.ToString();
                XML += "</CreatedOnUtc>";

                XML += "<ParentCategoryId>";
                XML += item.ParentCategoryId.ToString();
                XML += "</ParentCategoryId>";

                XML += "</Category>";
            }
            XML += "</Collection>";

            #endregion

            return XML;

        }
    }