0
我有一個看起來像這樣的數據:追加多元素的XmlDocument
List<Deal> deals = new List<Deal>
{
new Deal
{
DealNo = "S001",
DealItem = new List<string> { "A001", "A002", "A003" }
},
new Deal
{
DealNo = "S002",
DealItem = new List<string> { "T001", "T002" }
}
};
而且我想我的輸出看起來像這樣:
<Resource>
<ResourceID>
<ResourceBody>
<DealNo>S001</DealNo>
<ItemList>
<Item>A001</Item>
<Item>A002</Item>
<Item>A003</Item>
</ItemList>
<DealNo>S002</DealNo>
<ItemList>
<Item>T001</Item>
<Item>T002</Item>
</ItemList>
</ResourceBody>
</ResourceID>
</Resource>
我被困在試圖循環通過ItemList元素並添加節點。這是我現在有:
var xdoc = new XmlDocument();
// This structure is from other parts of the system, I can't change this
xdoc.LoadXml("<Resource><ResourceID><ResourceBody></ResourceBody></ResourceID></Resource>");
foreach (var deal in deals)
{
var node = xdoc.CreateNode("element", "DealNo", "");
node.InnerText = deal.DealNo;
var singleNode = xdoc.SelectSingleNode("Resource/ResourceID/ResourceBody");
singleNode.AppendChild(node);
node = xdoc.CreateElement("ItemList");
singleNode = xdoc.SelectSingleNode("Resource/ResourceID/ResourceBody");
singleNode.AppendChild(node);
}
如何循環所有'交易',並追加通過所有ItemList元素?我不得不使用XmlDocument,它是舊代碼的一部分。
嘿,謝謝!我的大腦有點卡住了,我真的無法想象那一個。再次感謝:) 現在把這個邏輯轉換成C/AL ......(我最初在動態導航中遇到了這個問題,我只是把C#放在這裏,因爲觀衆更廣泛) – Farid