Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
221 views
in Technique[技术] by (71.8m points)

c# - Parent/Children Xml to DTO Object Model with LINQ

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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()
     }
);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...