【问题标题】:Build XML from table [closed]从表构建 XML [关闭]
【发布时间】:2018-03-06 22:09:54
【问题描述】:

我有一个重复OrderIds 的表,但在行中也有详细信息。我正在使用 StringBuilder 创建 XML。

如何使用for-each 循环构建XML 字符串。我不想重复标题信息,只循环详细了解那个独特的OrderId 有多少行。

表格中的示例数据

OrderId - CustomerId - ItemId - Price
123       JONES        838483   3.99
123       JONES        859300   4.99
874       ROCKY        838483   1.99
874       ROCKY        838543   2.99

期望的输出:

foreach (var o in sdr)

应用于以下 XML:

<orders>
    <order>
        <orderid>123</orderid>
        <Customerid>JONES</CustomerId>
        <items>
            <itemid>838483</itemid>
            <price>3.99</price>
            <itemid>859300</itemid>
            <price>4.99</price>
        </items>
    </order>
    <order>
        <orderid>874</orderid>
        <Customerid>ROCKY</CustomerId>
        <items>
            <itemid>838483   </itemid>
            <price>1.99</price>
            <itemid>838543</itemid>
            <price>2.99</price>
        </items>
    </order>
</orders>

这是我的尝试

foreach(sdrForEach 中的 var o) {

                 string currentOrderId = sdrForEach.GetValue(0).ToString();


                 countcycle = countcycle + 1;

                 if (countcycle == 1)
                 {
                         string orderId = sdrForEach.GetValue(0).ToString();
                         string account = sdrForEach.GetValue(1).ToString();
                         string customerId = sdrForEach.GetValue(2).ToString();
                         string orderType = sdrForEach.GetValue(3).ToString();
                         string fullname = sdrForEach.GetValue(4).ToString();
                         string address1 = sdrForEach.GetValue(5).ToString();
                         string address2 = sdrForEach.GetValue(6).ToString();
                         string city = sdrForEach.GetValue(7).ToString();
                         string state = sdrForEach.GetValue(8).ToString();

                         string phone1 = sdrForEach.GetValue(23).ToString();
                         string phone2 = sdrForEach.GetValue(24).ToString();
                         string phone3 = sdrForEach.GetValue(25).ToString();
                         if (String.IsNullOrEmpty(phone1)) { phone1 = ""; }
                         if (String.IsNullOrEmpty(phone2)) { phone2 = ""; }
                         if (String.IsNullOrEmpty(phone3)) { phone3 = ""; }

                         string notes = sdrForEach.GetValue(28).ToString();
                         if (String.IsNullOrEmpty(notes)) { notes = ""; }


                     myStringXml.Append("<service_orders> ");
                     myStringXml.Append("<service_order> ");
                     myStringXml.Append("<number>" + orderId + "</number> ");
                     myStringXml.Append("<account>" + account +"</account>");
                     myStringXml.Append("<service_type>" + orderType + "</service_type>");
                     myStringXml.Append("<description></description>");
                     myStringXml.Append("<customer>");
                     myStringXml.Append("<customer_id>" + customerId + "</customer_id>");
                     myStringXml.Append("<first_name>" + fullname + "</first_name>");
                     myStringXml.Append("<last_name>" + fullname + "</last_name>");
                     myStringXml.Append("<email>" + orderType + "</email>");
                     myStringXml.Append("<phone1>" + phone1 + "</phone1>");
                     myStringXml.Append("<phone2>" + phone2 + "</phone2>");
                     myStringXml.Append("<phone3>" + phone3 + "</phone3>");
                     myStringXml.Append("<address1>" + address1 + "</address1>");
                     myStringXml.Append("<address2>" + address2 + "</address2>");
                     myStringXml.Append("<city>" + city + "</city>");
                     myStringXml.Append("<state>" + state + "</state>");
                     myStringXml.Append("<zip>" + orderType + "</zip>");
                     myStringXml.Append("<latitude></latitude>");
                     myStringXml.Append("<longitude></longitude>");
                     myStringXml.Append("</customer>");
                     myStringXml.Append("<notes count=" + Constants.dispatchObjects.DOUBLE_QUOTE + Constants.dispatchObjects.NOTECOUNT + Constants.dispatchObjects.DOUBLE_QUOTE + ">");
                     myStringXml.Append("<note created_at='' author=''>" + notes + "</note>");
                     myStringXml.Append(" </notes>");

                 }



                       string itemPrice = sdrForEach.GetValue(12).ToString();
                       string itemId = sdrForEach.GetValue(13).ToString();
                       string itemDescription = sdrForEach.GetValue(16).ToString();
                       string itemSequence = sdrForEach.GetValue(29).ToString();
                       string qty = sdrForEach.GetValue(14).ToString();
                       string volume = sdrForEach.GetValue(19).ToString();
                       string inventoryType = sdrForEach.GetValue(21).ToString();
                       string storeStop = sdrForEach.GetValue(17).ToString();
                       string setupTime = sdrForEach.GetValue(18).ToString();
                       if (String.IsNullOrEmpty(setupTime)){setupTime = "";}





                 myStringXml.Append("<items>");
                 myStringXml.Append("<item>");
                       myStringXml.Append("<sale_sequence>"+ itemSequence +"</sale_sequence>");
                       myStringXml.Append("<item_id>"+ itemId +"</item_id>");
                       myStringXml.Append("<serial_number></serial_number>");
                       myStringXml.Append("<description>" + itemDescription + "</description>");
                       myStringXml.Append("<quantity>" + qty +"</quantity>");
                       myStringXml.Append("<location></location>");
                       myStringXml.Append("<cube>" + volume +"</cube>");
                       myStringXml.Append("<setup_time>" + setupTime + "</setup_time>");
                       myStringXml.Append("<weight></weight>");
                       myStringXml.Append("<price>"+ itemPrice +"</price>");
                       myStringXml.Append("<countable>"+ inventoryType +"</countable>");
                       myStringXml.Append("<store_code>" + storeStop +"</store_code>");

                  myStringXml.Append("</item>");
                  myStringXml.Append("<items>");



             }



             myStringXml.Append("</service_order>");
             myStringXml.Append("</service_orders>");

【问题讨论】:

  • 这是在数据库表中吗?什么平台?
  • 不要使用字符串生成器来构建 XML。
  • 是的,sql 数据库。使用 sql cmd 和 con 的传统调用,尝试从 stringbuilder .net 4.0 构建 xml
  • 如果您使用的是 SQL 服务器,那么最好的选择是使用FOR XML。如果您使用的是 .Net 4.0,请使用 LINQ to XML。您会在 MSDN 和 SO 上找到大量示例。
  • 仅供参考,称人们为失败者并不是交朋友和获得帮助的好方法。

标签: c# xml foreach stringbuilder


【解决方案1】:

要使用 LINQ 执行此操作,您可以执行以下操作。 (我建议为列命名而不是编号的索引器,但这会这样做)我还定义了自己的模式,以便我可以更快地完成它。你可以按照模式,把它变成你喜欢的任何形状。

var table = new DataTable(); //your table here

var orders = from row in table.Rows.Cast<DataRow>()
             let item = new
             {
                 OrderID = row[0],
                 Account = row[1],
                 ItemID = row[999],
                 Price = row[998],
             }
             group item by new { item.OrderID, item.Account };

var xml = new XElement("orders",
    from order in orders
    select new XElement("order",
        new XAttribute("id", order.Key.OrderID),
        new XAttribute("account", order.Key.Account),
        new XElement("items",
            from item in order
            select new XElement("item",
                new XAttribute("id", item.ItemID),
                new XAttribute("price", item.Price)
            )
        )
    )
);

【讨论】:

    【解决方案2】:

    啊,再次... XML 不是字符串。时期。实际上,它是一个数据缓冲区,在 80% 的情况下看起来类似于字符串,其他 20% 会伤害你。

    可以做些什么呢?至少有两个选择:

    由于自动序列化/反序列化,第一个选项最方便,第二个更灵活(因为并非所有 XML 模式都可以通过序列化属性实现)。并且永远不要将您的 XML 组合为“逐块字符串”,因为您可能会遇到格式错误的 XML、不正确的 XML 内容编码和许多其他问题。编码愉快!

    【讨论】:

      猜你喜欢
      • 2014-09-26
      • 1970-01-01
      • 1970-01-01
      • 2013-03-13
      • 1970-01-01
      • 2013-07-30
      • 2013-09-15
      • 1970-01-01
      • 2013-11-12
      相关资源
      最近更新 更多