家长/孩子的XML使用LINQ DTO对象模型 [英] Parent/Children Xml to DTO Object Model with LINQ

查看:120
本文介绍了家长/孩子的XML使用LINQ DTO对象模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于以下DTO定义:

  [Serializable接口] 
内部类OrderCollection:列表<排序>
{}

[Serializable接口]
内部类订单
{
公共字符串的OrderId {搞定;组; }
公共OrderDetailCollection订单明细{搞定;组; }
}


[Serializable接口]
内部类OrderDetailCollection:列表<&的OrderDetail GT;
{}

[Serializable接口]
内部类的OrderDetail
{
内部的OrderDetail()
{
}

/ *公共字符串ParentOrderId {搞定;组; } * /
公共字符串ITEMNAME {搞定;组; }
公众诠释数量{搞定;组; }
}

和下面的XML:

 <根和GT; 
<订单>

<为了订单ID =ABC123>
<&ORDERDETAILS GT;

<的OrderDetail ITEMNAME =自行车量=1/>
<的OrderDetail ITEMNAME =泰迪熊量=2/>
<的OrderDetail ITEMNAME =娃娃数量=3/>

< / ORDERDETAILS>
< /排序>
<! - - >
<为了订单ID =DEF234>
<&ORDERDETAILS GT;

<的OrderDetail ITEMNAME =卡车数量=4/>
<的OrderDetail ITEMNAME =弹珠数量=10/>
<的OrderDetail ITEMNAME =棋盘游戏量=6/>

< / ORDERDETAILS>
< /排序>

< /订单>
< /根>



有没有一种方法来填充整个对象模型(导致使用LINQ型OrderCollection的一个对象。 ....在一通?



下面是我........(我能得到的订单(S)人口)......但不知道如何让孩子们?或者我必须采取2通行证它,然后投其所好起来?

  XDOC的XDocument = XDocument.Load(fullFileName); 

// NS的XNamespace = XNamespace.Get(http://schemas.microsoft.com/developer/msbuild / 2003);
串NS =的String.Empty;

名单<排序>了LineItem =新的List<排序>
(从xDoc.Descendants列表
在list.Elements(NS +订单)
从项目(NS +序)
,其中项目!= NULL
选择新订单
{
的OrderId = item.Attribute(订单ID)== NULL的String.Empty:?item.Attribute(订单ID)值
}
);


OrderCollection returnCollection =新OrderCollection();
returnCollection.AddRange(了LineItem);


解决方案

试试这个:

 内部类OrderDetailCollection:列表<&的OrderDetail GT; 
{
内部OrderDetailCollection(){}
内部OrderDetailCollection(IEnumerable的<&的OrderDetail GT; SRC)
{
的AddRange(SRC);
}
}

名单,LT;排序>了LineItem =新的List<排序>
(从列表
在xDoc.Descendants(NS +订单),从项目
在list.Elements(NS +序)
,其中项目!= NULL
选择新订单
{
//注意,演员阵容比简单代码中的
//http://msdn.microsoft.com/en-us空支票写/library/bb387049.aspx
=的OrderId(串)item.Attribute(订单ID)的细节,
=订单明细新OrderDetailCollection(
在item.Descendants(的OrderDetail)
选择新的OrderDetail {
ITEMNAME =(字符串)detail.Attribute(ITEMNAME),
数量=(INT)detail.Attribute(量)
}

}
);

如果你不需要单独的类的集合,并且可以使用清单<排序> 列表<订单明细> 来代替,那么你可以这样做:

 列表<排序>了LineItem =新的List<排序> 
(从列表
在xDoc.Descendants(NS +订单),从项目
在list.Elements(NS +序)
,其中项目!= NULL
选择新订单
{
=的OrderId(串)item.Attribute(订单ID),
=订单明细($ b $从细节b在item.Descendants(的OrderDetail)
选择新的OrderDetail {
ITEMNAME =(字符串)detail.Attribute(ITEMNAME),
数量=(INT)detail.Attribute(量)
}
).ToList()
}
);


Given the below DTO definitions:

[Serializable]
internal class OrderCollection : List<Order>
{ }

[Serializable]
internal class Order
{
    public string OrderId { get; set; }
    public OrderDetailCollection OrderDetails { get; set; }
}


[Serializable]
internal class OrderDetailCollection : List<OrderDetail>
{ }

[Serializable]
internal class OrderDetail
{
    internal OrderDetail()
    {
    }

    /*public string ParentOrderId { get; set; }*/
    public string ItemName { get; set; }
    public int Quantity { get; set; }
}

and the following xml:

<root>
    <orders>

        <order orderId="ABC123">
            <orderDetails>

                <orderDetail itemName="Bike" quantity="1"/>
                <orderDetail itemName="TeddyBear" quantity="2"/>
                <orderDetail itemName="Doll" quantity="3"/>

            </orderDetails>
        </order>
        <!--  -->
        <order orderId="DEF234">
            <orderDetails>

                <orderDetail itemName="Truck" quantity="4"/>
                <orderDetail itemName="Marbles" quantity="5"/>
                <orderDetail itemName="BoardGame" quantity="6"/>

            </orderDetails>
        </order>

    </orders>
</root>

Is there a way to populate the entire object model (resulting in one object of type OrderCollection using Linq.....and in "one pass" ?

Here is what I have........(I can get the "Order(s)" populated)......but not sure how to get the children? Or do I have to take 2 passes at it and then "match them up" ?

    XDocument xDoc = XDocument.Load(fullFileName);

    //XNamespace ns = XNamespace.Get("http://schemas.microsoft.com/developer/msbuild/2003");
    string ns = string.Empty;

    List<Order> lineItems = new List<Order>
    (
            from list in xDoc.Descendants(ns + "orders")
            from item in list.Elements(ns + "order")
            where item != null
            select new Order
            {
                OrderId = item.Attribute("orderId") == null ? string.Empty : item.Attribute("orderId").Value
            }
       );


    OrderCollection returnCollection = new OrderCollection();
                returnCollection.AddRange(lineItems);

解决方案

Try this:

internal class OrderDetailCollection : List<OrderDetail>
{
    internal OrderDetailCollection() {}
    internal OrderDetailCollection(IEnumerable<OrderDetail> src)
    {
        AddRange(src);
    }
}

List<Order> lineItems = new List<Order>
(
    from list in xDoc.Descendants(ns + "orders")
    from item in list.Elements(ns + "order")
    where item != null
    select new Order
    {
        //note that the cast is simpler to write than the null check in your code
        //http://msdn.microsoft.com/en-us/library/bb387049.aspx
        OrderId = (string)item.Attribute("orderId"),
        OrderDetails = new OrderDetailCollection(
            from detail in item.Descendants("orderDetail")
            select new OrderDetail {
                ItemName = (string)detail.Attribute("itemName"),
                Quantity = (int)detail.Attribute("quantity")
            }
        )
     }
);

If you don't need separate classes for the collections, and can use List<Order> and List<OrderDetails> instead, then you can do this:

List<Order> lineItems = new List<Order>
(
    from list in xDoc.Descendants(ns + "orders")
    from item in list.Elements(ns + "order")
    where item != null
    select new Order
    {
        OrderId = (string)item.Attribute("orderId"),
        OrderDetails = (
            from detail in item.Descendants("orderDetail")
            select new OrderDetail {
                ItemName = (string)detail.Attribute("itemName"),
                Quantity = (int)detail.Attribute("quantity")
            }
        ).ToList()
     }
);

这篇关于家长/孩子的XML使用LINQ DTO对象模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆