使用XMLDocument C#xml添加多个节点

问题描述 投票:2回答:2

我正在尝试在我的XML中添加多个元素/节点(Invoice节点)。

这是xml结构:(必需输出)

<Request>
   <Operation>Testing</Operation>
   <Count>2</Count>
   <Params>
      <Invoice>     
        <field name="CustomerNo" value="20000" />
        <field name="Email" value="[email protected]" />
        <field name="Invoice" value="12345" />
      </Invoice>
      <Invoice>     
        <field name="CustomerNo" value="10000" />
        <field name="Email" value="[email protected]" />
        <field name="Invoice" value="54321" />
       </Invoice>
   </Params>
</Request>

这是我的代码

int[] invoice = new int[] {1002,1003};
foreach (int i in invoice)
{
  XDocument xDocument = new XDocument(
    new XDeclaration("1.0", "UTF-8", null),
      new XElement("Request",
        new XElement("Operation", "Testing"),
        new XElement("Count","2"),                               
        new XElement("Params",
          new XElement("Invoice",
            new XElement("Field",
              new XAttribute("name","CustomerNo"),
              new XAttribute("value","10000")),
            new XElement("Field",
              new XAttribute("name","Email"),
              new XAttribute("value","[email protected]")),
            new XElement("Field",
              new XAttribute("name","Invoice"),
              new XAttribute("value",i))))));   
   Console.WriteLine(xDocument);
}

上面的代码生成了这个:

<Request>
  <Operation>Testing</Operation>
  <Count>2</Count>
  <Params>
    <Invoice>
      <Field name="CustomerNo" value="10000" />
      <Field name="Email" value="[email protected]" />
      <Field name="Invoice" value="1002" />
    </Invoice>
  </Params>
</Request>
<Request>
  <Operation>Testing</Operation>
  <Count>2</Count>
  <Params>
    <Invoice>
      <Field name="CustomerNo" value="10000" />
      <Field name="Email" value="[email protected]" />
      <Field name="Invoice" value="1003" />
    </Invoice>
  </Params>
</Request>

我只想根据数组中的发票数量来循环发票节点部分。我知道我正在循环XML的整个结构,但我找不到在XMLDocument中插入循环的方法。

任何帮助将不胜感激!

谢谢!

c# xml
2个回答
2
投票

您可以用LINQ替换循环:

XDocument xDocument = new XDocument(
new XDeclaration("1.0", "UTF-8", null),
  new XElement("Request",
    new XElement("Operation", "Testing"),
    new XElement("Count","2"),                               
    new XElement("Params", invoices.Select(x =>
      new XElement("Invoice",
        new XElement("Field",
          new XAttribute("name","CustomerNo"),
          new XAttribute("value","10000")),
        new XElement("Field",
          new XAttribute("name","Email"),
          new XAttribute("value","[email protected]")),
        new XElement("Field",
          new XAttribute("name","Invoice"),
          new XAttribute("value",x)))))));  

2
投票

问题是您在循环中迭代完整的文档

int[] invoice = new int[] {1002,1003};
List<XElement> eleList = new List<XElement>();

foreach (int i in invoice)
{
  eleList.Add(
            new XElement("Invoice",
            new XElement("Field",
              new XAttribute("name","CustomerNo"),
              new XAttribute("value","10000")),
            new XElement("Field",
              new XAttribute("name","Email"),
              new XAttribute("value","[email protected]")),
            new XElement("Field",
              new XAttribute("name","Invoice"),
              new XAttribute("value",i)))
   );
}

XDocument xDocument = new XDocument(
    new XDeclaration("1.0", "UTF-8", null),
      new XElement("Request",
        new XElement("Operation", "Testing"),
        new XElement("Count","2"),                               
        new XElement("Params", eleList)));
© www.soinside.com 2019 - 2024. All rights reserved.