0

My Code

IList<Person> people=new List<Person>();
            people.Add(new Person{Id=1,Name="Nitin"});

        IList<decimal> my=new List<decimal>(){1,2,3};
        IList<int> your=new List<int>(){1,2,3};

        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"),
                                       new XElement("Barcode", Form1.GetStrings(1).Select(i => new XElement("Barcode", i)))
                                        ));
        MessageBox.Show(xml.ToString());

GetStrings only returns int from 1 to 4

Output

<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

Nitin Varpe
  • 10,450
  • 6
  • 36
  • 60
  • 6
    your ideal output isn't valid xml. It has a closing `` tag bug no corresponding opening tag. Also, it's missing the `` closing tag. Otherwise, I can't see any difference between your current output and ideal output – DaveDev Jan 21 '14 at 13:49
  • I dislike having such *one line that make everything*. You should decompose your Linq query into smaller chunks. IT would be easier to write, read and maintain. Having some extra code has often a very small computer cost compared to unreadable codE. – Steve B Jan 21 '14 at 13:51
  • Issue in only pasting – Nitin Varpe Jan 21 '14 at 13:52
  • @NitinVarpe `` still closed without opening – Sergey Berezovskiy Jan 21 '14 at 13:52
  • @SteveB actually i am having 5-6K objects in IList thats why I am implementing this. Sergey Updated thanx – Nitin Varpe Jan 22 '14 at 04:50

2 Answers2

4

Then instead of this:

new XElement("Barcode", Form1.GetStrings(1).Select(i => new XElement("Barcode", i)))

Use your query directly like this, don't create an extra Barcode element:

Form1.GetStrings(1).Select(i => new XElement("Barcode", i))

Then your code should look like 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))
                             ));

That will give you the expected output.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
0

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))
                         ));