---
title: "Generating Xelement from list of object"  
description: "Generating Xelement from list of object"  
author: "Anonymous User"  
published: 2014-01-22  
updated: 2014-01-23  
canonical: https://www.mindstick.com/forum/1859/generating-xelement-from-list-of-object  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# Generating Xelement from list of object

My [Code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii)

IList<Person> [people](https://www.mindstick.com/news/2295/more-people-need-to-monitor-this-crucial-metric-for-heart-health)=new List<Person>();

people.Add(new Person{Id=1,[Name](https://yourviews.mindstick.com/view/87450/how-to-generate-a-brand-name-using-linkedin-comprehensive-guide)="Nitin"});

IList<[decimal](https://www.mindstick.com/forum/34709/please-write-a-program-for-decimal-to-binary-conversion-in-c-sharp)> my=new List<decimal>(){1,2,3};

IList<[int](https://www.mindstick.com/forum/159137/how-to-convert-date-int-to-date-in-sql)> your=new List<int>(){1,2,3};

XElement [xml](https://www.mindstick.com/blog/203/working-with-xml-data-in-sql-server) = new XElement("people",

from p in people

[select](https://www.mindstick.com/forum/160534/orderby-then-select-vs-select-then-orderby-performance) new XElement("person", new XAttribute("Id", "Hello"),

new XElement("id", p.Id),

new XElement("Mrp", my.Contains(1) ? string.Join(",",my):"Nitin"),

new XElement("[Barcode](https://answers.mindstick.com/qa/49796/what-is-barcode-and-how-can-use-it)", Form1.GetStrings(1).Select(i => new XElement("Barcode", i)))

));

MessageBox.Show(xml.ToString());

GetStrings only returns int from 1 to 4

[Output](https://www.mindstick.com/interview/34427/explain-the-output-in-angular)

<people>

<person Id="Hello">

<id>1</id>

<Mrp>1,2,3</Mrp>

<Barcode>

<Barcode>1</Barcode>

<Barcode>2</Barcode>

<Barcode>3</Barcode>

<Barcode>4</Barcode>

</Barcode>

</person>

</people>

But I want output as

<people>

<person Id="Hello">

<id>1</id>

<Mrp>1,2,3</Mrp>

<Barcode>1</Barcode>

<Barcode>2</Barcode>

<Barcode>3</Barcode>

<Barcode>4</Barcode>

</person>

</people>

Any [Solutions](https://www.mindstick.com/articles/12807/product-searching-issues-and-solutions-for-ecommerce-store-advanced-search-autocomplete-suggest)

## Replies

### Reply by Pravesh Singh

Hi John,\

Try this :

```
XElement xml = new XElement("people",                   
from p in people                   
select new XElement("person", new XAttribute("Id",
"Hello"),                               new XElement("id",p.Id),
                               new XElement("Mrp", my.Contains(1) ?string.Join(",",my):"Nitin"),                              
Form1.GetStrings(1).Select(i => new XElement("Barcode", i))                        
));
```


---

Original Source: https://www.mindstick.com/forum/1859/generating-xelement-from-list-of-object

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
