c# - How do i make XML from a List? -


this question has answer here:

assume have class

public class test1 {     public int binid { get; set; }     public int number1 { get; set; }     public decimal holgraphic { get; set; }      public test1(int id, int num, decimal hol)     {         binid = id;         number1 = num;         holgraphic = hol;     } } 

assume have created list collection of test1 in class called test2. in test2 class have created collection t of test1.

list<test1> t = new list<test1>(); 

assume list contains 2 test1 objects. how convert list xml have following schema?

<t> <test1>   <binid>23</binid>   <number1>123</number1>   <holographic>2345.12</holographic> </test1> <test1>   <binid>3</binid>   <number1>346233</number1>   <holographic>12.345</holographic> </test1> </t> 

you can use linq generate xml. using classes xelement , xattribute, can generate xml file corresponding schema required.

please refer link example.

http://www.dotnetcurry.com/showarticle.aspx?id=428

something :

var xele = new xelement("t",                 ele in t                 select new xelement("test1",                              new xelement("binid", ele.id),                                new xelement("number1", ele.number1),                                new xelement("holographic", ele.holigraphic)                            ));      xele.save("d:\\yourfile.xml");     console.writeline("converted xml"); 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -