我的目标
我需要根据 Placement_ID 和 Fill_ID 的组合创建一个在我的文档中出现动态次数的 XElement。如果我的对象列表中的记录与另一条记录具有相同的 Placement_ID,我想在前一个下方附加另一个具有不同 Fll_ID 的填充 XElement。下面的例子:
<envelope ack="entity" transaction="multi">
<auth>
<user>TM_DEV</user>
<session>session1</session>
</auth>
<Trading op="createOrder, pretradeCpl, sendToTrading,createPlacement, createFill" id="os1">
<order op="create" id="test1234">
<scenario id="sample" />
<execBroker>BEAR</execBroker>
<ticker>MSFT</ticker>
<trader>TM_DEV</trader>
<instruction>LIM</instruction>
<limitPrice>85</limitPrice>
<transType>BUYL</transType>
<orderDuration>GTC</orderDuration>
<tradeDate>
</tradeDate>
<settleDate>
</settleDate>
<allocation id="" op="create">
<acctCd>tstacc00006</acctCd>
<targetQty>300</targetQty>
<specialInst />
</allocation>
<placement id="Place1" op="create">
<execBroker>BEAR</execBroker>
<placeQty>300</placeQty>
<fill id="fill1" op="create">
<fillQty>150</fillQty>
<fillPrice>43</fillPrice>
</fill>
<fill id="fill2" op="create">
<fillQty>150</fillQty>
<fillPrice>44</fillPrice>
</fill>
</placement>
</order>
</Trading>
我的进步
到目前为止,我编写的下面的代码查询一个列表对象并为每一行生成一个新顺序。我需要能够按 Placement_ID 对我的列表进行分组,如果它们是具有相同 Placement_ID 的两行,那么我希望它编写多个 Fill 元素。否则,如果没有重复的 Placement_ID,则列表中的每一行数据将有 1 个订单、1 个放置和 1 个填充。
XDocument doc = new XDocument(
new XDeclaration("1.0", "UTF-8", "yes"),
new XElement("envelope",
new XAttribute("ack", "entity"),
new XAttribute("transaction", "multi"),
new XElement("auth",
new XElement("user", "TM_DEV"),
new XElement("session", "session1")),
new XElement("trading",
new XAttribute("op", "createOrder, pretradeCpl, sendToTrading, createPlacement, createFill"),
new XAttribute("id", "os1"),
from t in trades.ToList()
select new XElement("order",
new XAttribute("op", "create"),
new XAttribute("id", DateTime.Now.ToString("yyMMddHHmmssfff") + t.OrderId),
new XElement("Scenario",
new XAttribute("id", "sample")),
new XElement("execBroker", t.ExecBrkID),
new XElement("ticker", t.Ticker),
new XElement("sedol", t.Sedol),
new XElement("Trader", "TM_DEV"),
new XElement("transType", t.TransType),
new XElement("orderDuration", "GTC"),
new XElement("tradeDate", ""),
new XElement("settleDate", ""),
new XElement("allocation",
new XAttribute("id", ""),
new XAttribute("op", "create"),
new XElement("acctCd", t.Account),
new XElement("targetQty", t.TargetQty),
new XElement("specialInst", "who to settle with")),
new XElement("placement",
new XAttribute("op", "create"),
new XAttribute("id", t.PlacementId),
new XElement("execBroker", t.ExecBrkID),
new XElement("placeQty", t.ExecQty),
new XElement("fill",
new XAttribute("id", t.FillId),
new XAttribute("op", "create"),
new XElement("fillQty", t.ExecQty),
new XElement("fillPrice", t.ExecPrice)))))));
更新
我已经使用您的建议分解了更新中的示例。现在我收到“文档格式不正确”的错误。
using (XmlWriter xw = XmlWriter.Create(s, xws)){
XElement order = new XElement("order");
var groupedOrders = trades.GroupBy(o => o.OrderId);
foreach (var groupie in groupedOrders)
{
int n = 1;
XElement root = new XElement("placement",
from g in groupie
select new XElement("t",
new XAttribute("op", "create"),
new XAttribute("id", g.Ticker)));
foreach (var fill in groupie)
{
XElement newFillElement = new XElement("fill" + n,
new XAttribute("id", fill.PlacementId),
new XAttribute("op", "create"),
new XElement("fillQty", fill.ExecQty),
new XElement("fillPrice", fill.ExecPrice));
root.Add(newFillElement);
n++;
}
order.Add(root);
}
XDocument doc = new XDocument(
new XDeclaration("1.0", "UTF-8", "yes"), order);
doc.Save(xw);
}