c# - Appending multi-element to XmlDocument -


i have data this:

list<deal> deals = new list<deal> {     new deal     {         dealno = "s001",         dealitem = new list<string> { "a001", "a002", "a003" }     },     new deal     {         dealno = "s002",         dealitem = new list<string> { "t001", "t002" }     } }; 

and want output this:

<resource>   <resourceid>     <resourcebody>       <dealno>s001</dealno>       <itemlist>         <item>a001</item>         <item>a002</item>         <item>a003</item>       </itemlist>       <dealno>s002</dealno>       <itemlist>         <item>t001</item>         <item>t002</item>       </itemlist>     </resourcebody>   </resourceid> </resource> 

i'm stuck @ trying loop through itemlist element , adding nodes inside. have now:

var xdoc = new xmldocument(); // structure other parts of system, can't change xdoc.loadxml("<resource><resourceid><resourcebody></resourcebody></resourceid></resource>");  foreach (var deal in deals) {     var node = xdoc.createnode("element", "dealno", "");     node.innertext = deal.dealno;     var singlenode = xdoc.selectsinglenode("resource/resourceid/resourcebody");     singlenode.appendchild(node);      node = xdoc.createelement("itemlist");     singlenode = xdoc.selectsinglenode("resource/resourceid/resourcebody");     singlenode.appendchild(node); } 

how loop through 'deals' , append through itemlist elements?

constraints: forced work xmldocument, it's part of old codes.

using sample, add new loop add deal items:

foreach (var deal in deals) {     var node = xdoc.createnode("element", "dealno", "");     node.innertext = deal.dealno;     var singlenode = xdoc.selectsinglenode("resource/resourceid/resourcebody");     singlenode.appendchild(node);      node = xdoc.createelement("itemlist");     singlenode = xdoc.selectsinglenode("resource/resourceid/resourcebody");      // add deal items here...     foreach (string dealitem in deal.dealitem) {         var dealitemnode = xdoc.createnode("element", "item", "");         dealitemnode.innertext = dealitem;         node.appendchild(dealitemnode);     }      singlenode.appendchild(node); } 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -