使用linq或lambda创建XML字符串 [英] Create XML string using linq or lambda

查看:70
本文介绍了使用linq或lambda创建XML字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想创建一个如下所示的xml.

Hi,

I want to create a xml as given below.

<Root>
  <CustomerDetail>
    <Customer>
      <Id>CUST0001</Id>
      <Name>Jackson</Name>
      <Address>12th Street</Address>
      </Customer>
    <Customer>
      <Id>CUST0002</Id>
      <Name>Johnson</Name>
      <Address>15th Main Road</Address>
    </Customer>
  </CustomerDetail>
  <OrderDetail>
    <Placed>
      <Order>
        <CustomerId>CUST0001</CustomerId>
        <OrderId>ORDER0012345</OrderId>
        <Total>1000</Total>
        <Item>
          <ItemId>ITEM00025</ItemId>
          <Price>255</Price>
        </Item>
        <Item>
          <ItemId>ITEM00026</ItemId>
          <Price>745</Price>
        </Item>
      </Order>
      <Order>
        <CustomerId>CUST0002</CustomerId>
        <OrderId>ORDER0012346</OrderId>
        <Total>450</Total>
        <Item>
          <ItemId>ITEM00095</ItemId>
          <Price>450</Price>
        </Item>
      </Order>
    </Placed>
    <Delivered>
      <Order>
        <OrderId>ORDER0012385</OrderId>
        <Total>800</Total>
      </Order>
      <Order>
        <OrderId>ORDER0012305</OrderId>
        <Total>550</Total>
      </Order>
    </Delivered>
    <Removed>
      <Order>
        <OrderId>ORDER0012300</OrderId>
      </Order>
      <Order>
        <OrderId>ORDER0012301</OrderId>
      </Order>
    </Removed>
  </OrderDetail>
</Root>



我计划将像
这样的类对象发送



I planed to send as a class object like

public class Report
{
    public Report()
    {
    }
    public int CustId;
    public string name;
    public string address;

    public int OrderId;
    public int total;

    public int ItemId;
    public int price;

    public string operation;//Placed, Removed, Delivered
}



将上面的类对象转换为top中提到的格式的xml.

请帮助我实现它.

提前谢谢..

问候
Selvam S



Convert the above class objects into the xml of format mentioned in top.

Please help me to achieve it.

Thanks in advance..

Regards
Selvam S

推荐答案

private void LoadXML()
        {

            List<Report> customerList = new List<Report>();
            customerList.Add(new Report() { CustId = 1, name = "Jackson", address = "12th Street" });
            customerList.Add(new Report() { CustId = 2, name = "Johnson", address = "15th Main Road" });

            List<Report> orderList = new List<Report>();
            List<Report> itemList = new List<Report>();
            orderList.Add(new Report() { CustId = 1, OrderId = 1, total = 1000, operation = "Placed" });
            orderList.Add(new Report() { CustId = 1, OrderId = 2, total = 2000, operation = "Placed" });
            orderList.Add(new Report() { CustId = 1, OrderId = 3, total = 3000, operation = "Delivered" });
            orderList.Add(new Report() { CustId = 2, OrderId = 4, total = 4000, operation = "Delivered" });
            orderList.Add(new Report() { CustId = 2, OrderId = 5, operation = "Removed" });
            orderList.Add(new Report() { CustId = 2, OrderId = 6, operation = "Removed" });

            itemList.Add(new Report() { OrderId = 1, ItemId = 1, price = 1001 });
            itemList.Add(new Report() { OrderId = 1, ItemId = 2, price = 1002 });
            itemList.Add(new Report() { OrderId = 2, ItemId = 3, price = 1003 });
            itemList.Add(new Report() { OrderId = 2, ItemId = 4, price = 1004 });
            itemList.Add(new Report() { OrderId = 3, price = 1005 });
            itemList.Add(new Report() { OrderId = 3, price = 1006 });
            itemList.Add(new Report() { OrderId = 4, price = 1007 });
            itemList.Add(new Report() { OrderId = 4, price = 1008 });
            itemList.Add(new Report() { OrderId = 5 });
            itemList.Add(new Report() { OrderId = 5 });
            itemList.Add(new Report() { OrderId = 6 });
            itemList.Add(new Report() { OrderId = 6 });

            XElement xmlstring = new XElement("Root",
                new XElement("CustomerDetail",
                    customerList.Select(x => new XElement("Customer",
                        new XElement("Id", "CUST000" + x.CustId),
                        new XElement("Name", x.name),
                        new XElement("Address", x.address)))),
                        new XElement("OrderDetail", orderList.Any(x => x.operation == "Placed") ? 
                            AddTag(orderList.Where(s => s.operation == "Placed").ToList(), itemList, "Placed") : null,
                            orderList.Any(x => x.operation == "Delivered") ? 
                            AddTag(orderList.Where(s => s.operation == "Delivered").ToList(), itemList, "Delivered") : null,
                            orderList.Any(x => x.operation == "Removed") ? 
                            AddTag(orderList.Where(s => s.operation == "Removed").ToList(), itemList, "Removed") : null));
            System.Console.WriteLine(xmlstring.ToString());
        }

        private XElement AddTag(List<Report> order, List<Report> item, string operation)
        {
            return new XElement(operation,
                order.Select(x =>
                    new XElement("Order",
                        operation == "Placed" ? 
                        new XElement("CustomerId", "CUST000" + x.CustId) : null,
                        new XElement("OrderId", "ORDER000" + x.OrderId),
                        operation != "Removed" ? 
                        new XElement("Total", x.total) : null,
                        operation == "Placed" ? 
                        item.Where(y => y.OrderId == x.OrderId).Select(z =>
                            new XElement("Item",
                                new XElement("ItemId", "ITEM000" + z.ItemId),
                                new XElement("Price", z.price))) : null)));
        }


添加了代码块[/编辑]


Code block added[/Edit]


这篇关于使用linq或lambda创建XML字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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