-2

Assume i have the 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 i have created a List collection of Test1 in another class called Test2. In Test2 class i have created the collection t of Test1.

List<Test1> t = new List<Test1>();

Assume this List contains two Test1 objects. How do i convert the List to XML to have the 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>
0

1 Answer 1

1

You can use LINQ to generate the XML. Using the classes XElement and XAttribute, you can generate the XML file corresponding to the schema required.

Please refer this link for an example.

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

Something like :

var xEle = new XElement("t",
                from 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 to XML");
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.