繁体   English   中英

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

[英]Add multiple nodes using XMLDocument C# xml

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

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

<Request>
   <Operation>Testing</Operation>
   <Count>2</Count>
   <Params>
      <Invoice>     
        <field name="CustomerNo" value="20000" />
        <field name="Email" value="test2@yahoo.com" />
        <field name="Invoice" value="12345" />
      </Invoice>
      <Invoice>     
        <field name="CustomerNo" value="10000" />
        <field name="Email" value="test1@yahoo.com" />
        <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","testemail@yahoo.com")),
            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="testemail@yahoo.com" />
      <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="testemail@yahoo.com" />
      <Field name="Invoice" value="1003" />
    </Invoice>
  </Params>
</Request>

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

任何帮助将不胜感激!

谢谢!

您可以将循环替换为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","testemail@yahoo.com")),
        new XElement("Field",
          new XAttribute("name","Invoice"),
          new XAttribute("value",x)))))));  

问题是您要遍历循环中的完整文档

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","testemail@yahoo.com")),
            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)));

暂无
暂无

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

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