我有一个重复OrderId
的表,但在行中也有详细信息。我使用StringBuilder
来创建XML。
如何使用for-each
循环来构建XML来构建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>");
答案 0 :(得分: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)
)
)
)
);
答案 1 :(得分:0)
啊,再说一遍...... XML不是一个字符串。期。实际上,它是一个数据缓冲区,在80%的情况下看起来类似于字符串,而其他20%会对你造成很大伤害。
可以做些什么呢?至少有两个选择:
第一个选项最方便,因为自动序列化/反序列化,第二个选项更灵活(因为并非所有XML模式都可以通过序列化属性实现)。永远不要将你的XML作为一个&#34;字符串组成&#34;因为你可以得到格式错误的XML,不正确的XML内容编码和许多其他的痛苦。快乐的编码!