Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i am creating and XMLDocument and my code look like that:-

XNamespace SoapNs = "http://schemas.xmlsoap.org/soap/envelope/";
XNamespace TempNs = "http://tempuri.org/";
XNamespace ArrayNs= "http://schemas.microsoft.com/2003/10/Serialization/Arrays";

XDocument doc = new XDocument(
                     new XDeclaration("1.0", "utf-8", null),
                     new XElement(SoapNs + "Envelope",
                     new XAttribute(XNamespace.Xmlns + "soapenv", SoapNs),
                     new XAttribute(XNamespace.Xmlns + "tem", TempNs),
                     new XAttribute(XNamespace.Xmlns + "arr", ArrayNs),
                     new XElement(SoapNs + "Header"),
                     new XElement(SoapNs + "Body",
                     new XElement(TempNs + "MyData",
                     new XElement(TempNs + "Code", "Cirrus " + Guid.NewGuid().ToString()),

                     new XElement(TempNs + "Username", "testusername"),
                     new XElement(TempNs + "listoffiles",  new XElement(ArrayNs + "string",lst )),

                     new XElement(TempNs + "createdby", "System")))
                  )
             );


here i need to pass List of values in "listoffiles" node. and my xml will create for this value look like :-
<listoffiles>
  <arr:string>testfile1.csv</arr:string>
 <arr:string>testfile2.csv</arr:string>
</listoffiles>


What I have tried:

here is my code which i tried:-
List<string> lst = new List<string>();
      lst.Add("testfile1.csv");
      lst.Add("testfile2.csv");


i passes this list in my XMLDOcument code but it append the list values in a single row like this:-
<listoffiles>
  <arr:string>testfile1.csvtestfile2.csv</arr:string>
</listoffiles>


can any one suggest me the solution
Posted
Updated 4-Nov-22 2:02am

1 solution

You need one element per list item, not one element wrapping the entire list:
C#
new XElement(TempNs + "listoffiles", 
    lst.Select(s => new XElement(ArrayNs + "string", s))
),
 
Share this answer
 
Comments
TCS54321 4-Nov-22 8:09am    
Thank you so much. your solution work for me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900