none
LINQ to Nested XML RRS feed

  • Question

  • I have a LINQ query which returns data in the following format:

    RegionId  RegionName  PolygonId  Polygon
    1             North             0              coordinates0
    1             North             1              coordinates1
    1             North             2              coordinates2
    20           South             0              coordinates0
    20           South             1              coordinates1

    The XML I would like to produce looks like:
    <Regions>
       <Region>
          <RegionId>1</RegionId>
          <RegionName>North</RegionName>
          <Polygons>
             <Polygon>coodinates0</Polygon>
             <Polygon>coodinates1</Polygon>
             <Polygon>coodinates2</Polygon>
          </Polygons>
       </Region>
       <Region>
          <RegionId>20</RegionId>
          <RegionName>South</RegionName>
          <Polygons>
             <Polygon>coodinates0</Polygon>
             <Polygon>coodinates1</Polygon>
          </Polygons>
       </Region>
    </Regions>

    As I am very new to LINQ the best I can do so far is:

     

    XElement xml = new XElement("Regions",
    from RegionPolygon in result2 orderby RegionPolygon .DirId
    select (new XElement("Region",
    new XElement("RegionId", RegionPolygon.RegionId),
    new XElement("RegionName", RegionPolygon.RegionName),
    new
    XElement("Polygons" ,
    new XElement("Polygon",RegionPolygon.Polygon)))));

    This does not provide the nesting that I am  looking to create. Any insights would be gretrly approciated.

    Joe

    Wednesday, December 16, 2009 3:52 AM

Answers

  • Hello Joe,

     

    It’s my pleasure!   Because the result is retrieved by a stored procedure GetDirPolygon, I think my method is good.  J  You can directly use my approach like:

    =========================================================================================================
                var query = from r in db.GetDirPolygon()

                            group r by new { r.RegionId, r.RegionName } into list

                            select new

                            {

                                list.Key.RegionId,

                                list.Key.RegionName,

                                Polygon = list.OrderBy(li => li.PolygonId).Select(li => li.Polygon)

                            };

     

                XElement xml = new XElement("Regions",

                    from r in query

                    select new XElement("Region",

                        new XElement("RegionId", r.RegionId),

                        new XElement("RegionName", r.RegionName),

                        new XElement("Polygons",

                        from p in r.Polygon

                        select new XElement("Polygon", p))));


    =========================================================================================================

     

    If you have any questions, please be free to tell me, I will do my best to help.

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Joe Magrino Wednesday, December 16, 2009 6:48 PM
    Wednesday, December 16, 2009 1:33 PM
    Moderator

All replies

  • Hello Joe,

     

    Welcome to MSDN Forums!

     

    Is this a LINQ to SQL query?   If so, you can provide me with more detailed structure of the LINQ to SQL classes because maybe we can directly build a LINQ to SQL query based on the XML structure. 

     

    Currently, I make some sample codes based on a LINQ to Objects query for your references.  First, we group the result by RegionId and RegionName and then get a collection of anonymous type { RegionId, RegionName, Polygon (IEnumerable<string>)}:

    =========================================================================================================
                List<RegionPolygon> result = new List<RegionPolygon>()

                {

                    new RegionPolygon(1, "North", 0, "coordinates0"),

                    new RegionPolygon(1, "North", 1, "coordinates1"),

                    new RegionPolygon(1, "North", 2, "coordinates2"),

                    new RegionPolygon(20, "South", 0, "coordinates0"),

                    new RegionPolygon(20, "South", 1, "coordinates1"),

                };

     

                var query = from r in result

                            group r by new { r.RegionId, r.RegionName } into list

                            select new

                            {

                                list.Key.RegionId,

                                list.Key.RegionName,

                                Polygon = list.OrderBy(li => li.PolygonId).Select(li => li.Polygon)

                            };

     

                XElement xml = new XElement("Regions",

                    from r in query

                    select new XElement("Region",

                        new XElement("RegionId", r.RegionId),

                        new XElement("RegionName", r.RegionName),

                        new XElement("Polygons",

                        from p in r.Polygon

                        select new XElement("Polygon", p))));

    =========================================================================================================

        public class RegionPolygon

        {

            public int RegionId { get; set; }

            public string RegionName { get; set; }

            public int PolygonId { get; set; }

            public string Polygon { get; set; }

     

            public RegionPolygon(int rid, string name, int pid, string p)

            {

                this.RegionId = rid;

                this.RegionName = name;

                this.PolygonId = pid;

                this.Polygon = p;

            }

        }
    =========================================================================================================

     

    If you have any questions, please feel free to let me know.

     

    Have a great day!

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, December 16, 2009 5:30 AM
    Moderator
  • Thank you for the quick and comprehensive response. The data is actually returned vai a stored procedure (SQL Sever 2008). Shuld I use your response above and reference the returned results or is there a more efficient approach? Again, thanks! Here is the partiual class:
    public partial class GetDirPolygonResult
    {
     private int _DirId;
     private int _RegioId;
     private string _Name;
     private string _Polygon;
     
     public GetDirPolygonResult()
     {
     }
     
     [Column(Storage="_DirId", DbType="Int NOT NULL")]
     public int DirId
     {
      get
      {
       return this._DirId;
      }
      set
      {
       if ((this._DirId != value))
       {
        this._DirId = value;
       }
      }
     }
     
     [Column(Storage="_RegioId", DbType="Int NOT NULL")]
     public int RegioId
     {
      get
      {
       return this._RegioId;
      }
      set
      {
       if ((this._RegioId != value))
       {
        this._RegioId = value;
       }
      }
     }
     
     [Column(Storage="_Name", DbType="VarChar(20)")]
     public string Name
     {
      get
      {
       return this._Name;
      }
      set
      {
       if ((this._Name != value))
       {
        this._Name = value;
       }
      }
     }
     
     [Column(Storage="_Polygon", DbType="VarChar(MAX)")]
     public string Polygon
     {
      get
      {
       return this._Polygon;
      }
      set
      {
       if ((this._Polygon != value))
       {
        this._Polygon = value;
       }
      }
     }
    }
    Wednesday, December 16, 2009 12:47 PM
  • Hello Joe,

     

    It’s my pleasure!   Because the result is retrieved by a stored procedure GetDirPolygon, I think my method is good.  J  You can directly use my approach like:

    =========================================================================================================
                var query = from r in db.GetDirPolygon()

                            group r by new { r.RegionId, r.RegionName } into list

                            select new

                            {

                                list.Key.RegionId,

                                list.Key.RegionName,

                                Polygon = list.OrderBy(li => li.PolygonId).Select(li => li.Polygon)

                            };

     

                XElement xml = new XElement("Regions",

                    from r in query

                    select new XElement("Region",

                        new XElement("RegionId", r.RegionId),

                        new XElement("RegionName", r.RegionName),

                        new XElement("Polygons",

                        from p in r.Polygon

                        select new XElement("Polygon", p))));


    =========================================================================================================

     

    If you have any questions, please be free to tell me, I will do my best to help.

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Joe Magrino Wednesday, December 16, 2009 6:48 PM
    Wednesday, December 16, 2009 1:33 PM
    Moderator
  • Thank You! Works perfectly! Now I just need to learn exactly how it works - but that is my task.
    Wednesday, December 16, 2009 6:48 PM