C# XElemenet AddAfterSelf未添加到最后一个节点
创建XML的代码:C# XElemenet AddAfterSelf未添加到最后一个节点,c#,xml,xelement,C#,Xml,Xelement,创建XML的代码: public static string TagInvoice() { string InvoiceAll = ""; foreach (var item in QueryDb.ListaPostavkRacuna) { XElement Invoice = new XElement("Invoice",
public static string TagInvoice()
{
string InvoiceAll = "";
foreach (var item in QueryDb.ListaPostavkRacuna)
{
XElement Invoice = new XElement("Invoice",
new XElement("Identification",
new XElement("Structure", item.Structure),
new XElement("NrStructure", item.NrStructure)),
new XElement("ItemsDesc",
new XElement("DESC",
new XElement("DESC1","some_value"),
new XElement("DESC2", "ordinary"))));
for (int i = 1; i <= (int)Math.Ceiling(item.item_desc.Length / 35d); i++)
{
int Max = 35,
Min = 35 * i - Max;
if (item.item_desc.Length >= (Min + Max))
{
Min = Max * i - Max;
}
else
{
Max = item.item_desc.Length - (i - 1) * 35;
Min = (i - 1) * 35;
}
XElement TempItemDesc = new XElement("ItemsDesc",
new XElement("Code", item.code_art),
new XElement("DESC",
new XElement("DESC1", item.item_desc.Substring(Min, Max))));
Invoice.Element("ItemsDesc").AddAfterSelf(TempItemDesc);
}
InvoiceAll = InvoiceAll.ToString() + Invoice.ToString();
}
return InvoiceAll;
}
应添加如下节点:
"A"
"B"
"C"
"D"
增加了:
"D"
"C"
"B"
"A"
这是预期的行为吗?如何将节点以正确的顺序添加到列表中?如MSDN中所述
AddAfterSelf,在当前节点之后立即添加指定的内容
您将在第一个节点之后添加该节点。
请尝试以下代码:
Invoice.Element("ItemsDesc").Last().AddAfterSelf(TempItemDesc);
相关链接:如MSDN中所述
AddAfterSelf,在当前节点之后立即添加指定的内容
您将在第一个节点之后添加该节点。
请尝试以下代码:
Invoice.Element("ItemsDesc").Last().AddAfterSelf(TempItemDesc);
相关链接:这是
元素(…)
方法,该方法返回第一个名为的元素。您可以尝试使用Invoice.Elements(“ItemsDesc”).Last()
来代替。在我看来,它类似于Invoice.Add(TempItemDesc)
可以执行您想要的操作。它是元素(…)
方法,返回第一个名为的元素。您可以尝试使用Invoice.Elements(“ItemsDesc”).Last()
。在我看来,它类似于Invoice.Add(TempItemDesc)
可以执行您想要的操作。鉴于最后一个ItemsDesc
是元素内容的结尾,Invoice.Add(TempItemDesc)
在这里是等效的。如果我使用Invoice.Add,下一个元素将添加到当前标记中,这不是我想要的。我需要在…@rayroy之后添加它您的解决方案有效。在你的答案中有一个小的输入错误,它是元素而不是元素。谢谢你。@Tagyoureit不是吗?如果在ItemsDesc
元素上调用Add
,它将添加到ItemsDesc
中Invoice.Add
将添加为Invoice
的最后一个子节点,这正是您要执行的操作。请参阅。鉴于最后一个ItemDesc
是元素内容的结尾,Invoice.Add(TempItemDesc)
在这里是等效的。如果我使用Invoice.Add,下一个元素将添加到当前标记中,这不是我要查找的。我需要在…@rayroy之后添加它您的解决方案有效。在你的答案中有一个小的输入错误,它是元素而不是元素。谢谢你。@Tagyoureit不是吗?如果在ItemsDesc
元素上调用Add
,它将添加到ItemsDesc
中Invoice.Add
将添加为Invoice
的最后一个子节点,这正是您要执行的操作。看见