Reputation: 93
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!
Upvotes: 2
Views: 1648
Reputation: 593
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)));
Upvotes: 2
Reputation: 101681
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)))))));
Upvotes: 2