使用XMLDocument C#xml

时间:2018-08-29 17:59:39

标签: 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中插入循环的方法。

任何帮助将不胜感激!

谢谢!

2 个答案:

答案 0 :(得分: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","testemail@yahoo.com")),
        new XElement("Field",
          new XAttribute("name","Invoice"),
          new XAttribute("value",x)))))));  

答案 1 :(得分: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","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)));