2

I am trying to add multiple element/node (Invoice node) in my XML.

Here is the xml structure: (Required Output)

<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>

Here is my code

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);
}

The code above generates this:

<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>

I just want to loop the Invoice node part based on how many number of invoice in my array. I know I'm looping the entire structure of my XML, but I cannot find a way to insert my loop inside the XMLDocument.

Any help will be appreciated!

Thanks!

0

2 Answers 2

2

You can replace the loop with 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)))))));  
Sign up to request clarification or add additional context in comments.

1 Comment

Hi @Selman, it did work! Woow. I did not think you can do that! Thank you very much for your help!
2

Issue is that you are iterating the complete document in the loop

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)));

1 Comment

Hi @ManishM, this is an alternative solution to the problem! Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.