使用LINQ to XML从XML填充域对象

时间:2010-09-30 03:49:41

标签: c# linq-to-xml

我是LINQ to XML的新手,需要帮助将XML层次结构映射到我的域对象中。这是XML源:

<?xml version="1.0" encoding="UTF-8"?>
<Listings>
    <Region>United States</Region>
    <Listing>
        <CatID>ELE</CatID>
        <CatDesc>Electronics</CatDesc>
        <ItemID>ELE_LCDTV</ItemID>
        <ItemDesc>LCD TV BLU RAY</ItemDesc>
        <TotalPrice>1500</TotalPrice>
    </Listing>
    <Listing>
        <CatID>COMP</CatID>
        <CatDesc>Computer</CatDesc>
        <ItemID>COMP_LAPTOP</ItemID>
        <ItemDesc>Laptop HP</ItemDesc>
        <TotalPrice>1200</TotalPrice>
    </Listing>
    <Listing>
        <CatID>MISC</CatID>
        <CatDesc>Miscellaneous</CatDesc>
        <ItemID>MISC_WII</ItemID>
        <ItemDesc>Wii</ItemDesc>
        <TotalPrice>350</TotalPrice>
    </Listing>
    <Listing>
        <CatID>COMP</CatID>
        <CatDesc>Computer</CatDesc>
        <ItemID>COMP_HD</ItemID>
        <ItemDesc>Hard Disk</ItemDesc>
        <TotalPrice>300</TotalPrice>
    </Listing>
    <Listing>
        <CatID>ELE</CatID>
        <CatDesc>Electronics</CatDesc>
        <ItemID>ELE_IPOD</ItemID>
        <ItemDesc>iPod</ItemDesc>
        <TotalPrice>225</TotalPrice>
    </Listing>
    <Listing>
        <CatID>COMP</CatID>
        <CatDesc>Computer</CatDesc>
        <ItemID>COMP_WKEY</ItemID>
        <ItemDesc>Wireless Keyboard</ItemDesc>
        <TotalPrice>110</TotalPrice>
    </Listing>
    <Listing>
        <CatID>MISC</CatID>
        <CatDesc>Miscellaneous</CatDesc>
        <ItemID>MISC_GAME</ItemID>
        <ItemDesc>Games</ItemDesc>
        <TotalPrice>50</TotalPrice>
    </Listing>
</Listings>

我必须填充以下关注XML的域对象。基本上我必须公开一个IEnumerable ListCategories()

public class Category
{
    public string ID { get; set; }
    public string Description { get; set; }
    public IList<Item> Items { get; set; } 
}

public class Item
{
    public string ID { get; set; }
    public string Description { get; set; }
    public decimal TotalPrice { get; set; }

}

我不知道我必须首先使用orderby查询按CatID对XML进行排序,然后遍历它以填充我的域对象。

XDocument xDoc = XDocument.Parse(XmlizedString);

var listing = from x in xDoc.Elements("Listings").Elements("Listing")
                                           orderby (string)x.Element("CatID")
                                           select x;

上面的查询将按照CatID对我的XML进行排序,但我不清楚如何继续进行...

我非常感谢您在解决此问题时的建议/帮助。

2 个答案:

答案 0 :(得分:3)

您的Linq-to-xml查询将如下所示。

var listing = 
xDoc.Elements("Listings")
.Elements("Listing")
.GroupBy (x => new { 
                ID = x.Element(XName.Get("CatID")).Value
                , Description = x.Element(XName.Get("CatDesc")).Value
            }
)
.Select (x => new Category { 
                ID = x.Key.ID
                , Description = x.Key.Description
                , Items = x.Select (i => new Item { 
                                    ID = i.Element("ItemID").Value
                                    , Description = i.Element("ItemDesc").Value
                                    , TotalPrice = decimal.Parse(i.Element("TotalPrice").Value) 
                                   }
                    ).ToList()
            }
        )
.OrderBy (x => x.ID);

答案 1 :(得分:0)

我不知道Linq-to-xml,但是,如果上面的查询遍历你的xml并返回一个IEnumerable或类似的东西,你可以用以下方式填充你的对象:

XDocument xDoc = XDocument.Parse(XmlizedString);

var listing = from x in xDoc.Elements("Listings").Elements("Listing")
              orderby (string)x.Element("CatID")
              select new Category {
                  ID = x.CatID, 
                  Description = x.CatDesc, 
                  Items = new Item { 
                      x.ItemID,  
                      Description = x.ItemDesc, 
                      TotalPrice = x.TotalPrice 
                  } 
              };