简体   繁体   English

通过for循环(XDocument)将一个XML文件插入另一个XML文件

[英]Inserting one XML file into another via for loop (XDocument)

This is something I haven't done before, so I might be doing it wrong to begin with, if I am, please let me know. 这是我之前从未做过的事情,因此,如果我开始做的话,可能做错了,请告诉我。

I created a root XML file with XDocument with: 我用XDocument用以下命令创建了一个根XML文件:

public void SaveReceipt(List<ArticleFull> articles)
{
    XDocument receipt = new XDocument(
        new XDeclaration("1.0", "utf-8", null),
        new XElement("FiscalRecipet")
    );
    ...
}

And then with a for loop tried to iterate through the articles List so I can build a receipt XML file. 然后使用for循环尝试遍历articles List,以便我可以构建收据XML文件。

for (int i = 0; i < articles.Count; i++)
{
    int quantity = 1;
    //This while loop just counts how many items are in a row
    while(!(i + 1 > articles.Count - 1))
    {
        if (articles[i].Id == articles[i + 1].Id)
        {
            quantity++;
            i++;
        }
        else break;
    }
    var enternode = new XElement("FiscalItem",
        new XElement("Name", articles[i].Name.ToString()),
        new XElement("Price", articles[i].Price.ToString()),
        new XElement("Quantity", quantity.ToString()),
        new XElement("TaxGroup", articles[i].TaxGroup)
    );
    //Code missing here...
}

Now, as stated above, I have absolutely no idea how to connect one the receipt variable to the enternode variable. 现在,如上所述,我完全不知道如何将一个receipt变量连接到enternode变量。

The end XML file should look something like: 最终的XML文件类似于:

<?xml version="1.0" encoding="UTF-8"?>
<FiscalRecipet>
    <FiscalItem>
        <Name>Coffee</Name>
        <Price>130.0</Price>
        <Quantity>1.0</Quantity>
        <TaxGroup>2</TaxGroup>
    </FiscalItem>
    <FiscalItem>
        <Name>Chocolate</Name>
        <Price>350.0</Price>
        <Quantity>2.0</Quantity>
        <TaxGroup>3</TaxGroup>
    </FiscalItem>
    <!-- etc... -->
</FiscalRecipet>

I tried searching for an answer by googling, but I didn't get very lucky, which is why I'm thinking I might be going in a wrong direction(?) 我尝试通过谷歌搜索来寻找答案,但我并没有感到非常幸运,这就是为什么我认为自己可能会走错方向(?)

It sounds like you just need: 听起来您只需要:

receipt.Root.Add(enternode);

In other words, adding the new element to the root element of the document. 换句话说,将新元素添加到文档的根元素。

There are likely to be rather simpler ways of doing all of this using LINQ, btw. 使用LINQ,btw,可能有更简单的方法来完成所有这些工作。 I suspect you want something like: 我怀疑您想要类似的东西:

var articlesById = articles
   .GroupBy(article => article.Id)
   .Select(g => new { Article = g.First(), Count = g.Count() });
XDocument receipt = new XDocument(
    new XDeclaration("1.0", "utf-8", null),
    new XElement("FiscalRecipet",
        articlesById.Select(x => new XElement("FiscalItem",
            new XElement("Name", x.Article.Name),
            new XElement("Price", x.Article.Price),
            new XElement("Quantity", x.Count),
            new XElement("TaxGroup", x.Article.TaxGroup))
    )
);

That replaces your whole code. 那将替换您的整个代码。

Just add the fiscal items as children of your fiscal receipt: 只需将财务项目添加为财务收据的子项即可:

var fiscalReceipt = new XElement("FiscalRecipet");
var receipt = new XDocument(new XDeclaration("1.0", "utf-8", null), fiscalReceipt);

for (int i = 0; i < 5; i++)
{
    var enternode = new XElement("FiscalItem",
        new XElement("Name", "foo"),
        new XElement("Price", i * 100),
        new XElement("Quantity", 1),
        new XElement("TaxGroup", "A"));

    fiscalReceipt.Add(enternode);
}

Console.WriteLine(receipt);

<FiscalRecipet>
  <FiscalItem>
    <Name>foo</Name>
    <Price>0</Price>
    <Quantity>1</Quantity>
    <TaxGroup>A</TaxGroup>
  </FiscalItem>
  <FiscalItem>
    <Name>foo</Name>
    <Price>100</Price>
    <Quantity>1</Quantity>
    <TaxGroup>A</TaxGroup>
  </FiscalItem>
  <FiscalItem>
    <Name>foo</Name>
    <Price>200</Price>
    <Quantity>1</Quantity>
    <TaxGroup>A</TaxGroup>
  </FiscalItem>
  <FiscalItem>
    <Name>foo</Name>
    <Price>300</Price>
    <Quantity>1</Quantity>
    <TaxGroup>A</TaxGroup>
  </FiscalItem>
  <FiscalItem>
    <Name>foo</Name>
    <Price>400</Price>
    <Quantity>1</Quantity>
    <TaxGroup>A</TaxGroup>
  </FiscalItem>
</FiscalRecipet>
Press any key to continue . . .

Considering that FiscalReceipt is the root, and FiscalItem is your element in it, what you can do is try method 考虑到FiscalReceipt是根,而FiscalItem是其中的元素,您可以做的是try方法

root.Add(childElement);

This will add every FiscalItem as a child of FiscalReceipt. 这会将每个FiscalItem添加为FiscalReceipt的子级。

MSDN link on same : https://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.add%28v=vs.110%29.aspx 相同的MSDN链接: https : //msdn.microsoft.com/zh-cn/library/system.xml.linq.xdocument.add%28v=vs.110%29.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM