tags:

views:

73

answers:

1

Hi,

Iv got two DB tables. One containing types(Id, Name) and the other holds datapoints (RefId, Date, Value) that are referenced by the types. I need to create a XML file with the following strukture:

<?xml version='1.0' encoding='utf-8' ?>
<root>
   <type>
      <name></name>
      <data>
         <date></date>
         <temp></temp>
      </data>
      <data>
         <date></date>
         <temp></temp>
      </data>
      <data>
         <date></date>
         <temp></temp>
      </data>
   </type>
</root> 

And iv got the following code to do this

    public XmlDocument HelloWorld()
    {
        string tmp = "";
        try
        {
            sqlConn.ConnectionString = ConfigurationManager.ConnectionStrings["NorlanderDBConnection"].ConnectionString;
            DataContext db = new DataContext(sqlConn.ConnectionString);

            Table<DataType> dataTypes = db.GetTable<DataType>();
            Table<DataPoints> dataPoints = db.GetTable<DataPoints>();

            var dataT =
                        from t in dataTypes
                        select t;
            var dataP =
                from t in dataTypes
                join p in dataPoints on t.Id equals p.RefId
                select new
                {
                    Id = t.Id,
                    Name = t.Name,
                    Date = p.PointDate,
                    Value = p.PointValue
                };

            string xmlString = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><root></root>";

            XmlDocument xmldoc = new XmlDocument();

            xmldoc.LoadXml(xmlString);

            int count = 0;
            foreach (var dt in dataT)
            {
                XmlElement type = xmldoc.CreateElement("type");
                XmlElement name = xmldoc.CreateElement("name");
                XmlNode nameText = xmldoc.CreateTextNode(dt.Name);
                name.AppendChild(nameText);
                type.AppendChild(name);                    

                foreach(var dp in dataP.Where(dt.Id = dp.RefId))
                {
                    XmlElement data = xmldoc.CreateElement("data");
                    XmlElement date = xmldoc.CreateElement("date");
                    XmlElement temp = xmldoc.CreateElement("temp");

                    XmlNode dateValue = xmldoc.CreateTextNode(dp.Date.ToString());
                    date.AppendChild(dateValue);

                    XmlNode tempValue = xmldoc.CreateTextNode(dp.Value.ToString());
                    temp.AppendChild(tempValue);

                    data.AppendChild(date);
                    data.AppendChild(temp);

                    type.AppendChild(data);
                }

                xmldoc.DocumentElement.AppendChild(type);

            }

            return xmldoc;
        }
        catch(Exception e)
        {
            tmp = e.ToString();
        }

        return null;
    }

    [Table(Name="DataTypes")]
    public class DataType
    {
        [Column(IsPrimaryKey = true)]
        public long Id;
        [Column]
        public string Name;
    }
    [Table(Name="DataPoints")]
    public class DataPoints
    {
        [Column]
        public long RefId;
        [Column]
        public DateTime PointDate;
        [Column]
        public double PointValue;
    }

This is not a working code. Im having problems with LINQ and the inner joins. Could someone please help me to get the correct strukture. I hope its kinda clear what im trying to achive.

Best Regards Marthin

+3  A: 
var result =
    new XDocument(new XElement("root",
        from dt in dataTypes
        join dp in dataPoints on dt.Id equals dp.RefId
        select new XElement("type",
            new XElement("name", dt.Name),
            new XElement("data",
                new XElement("date", dp.PointDate),
                new XElement("temp", dp.PointValue)))));

var result =
    new XDocument(new XElement("root",
        from dt in dataTypes
        select new XElement("type",
            new XElement("name", dt.Name),
            from dp in dataPoints
            where dp.RefId == dt.Id
            select new XElement("data",
                new XElement("date", dp.PointDate),
                new XElement("temp", dp.PointValue)))));
dtb
Thank you for this. I havent tried it yet, but it seems to do the trick =). So you can combine LINQ and XML in the same "query"? Got any linqs for a newbie that explains this a bit more?
Marthin
@Marthin: I'm no sure if you can combine LINQ-to-SQL and LINQ-to-XML in one query, but you should be able to combine LINQ-To-Objects and LINQ-to-XML in the shown way. (I.e., you may need to add a AsEnumerable after the join clause.)
dtb
Yes, you can glue IEnumberable objects together in LINQ world.
PerlDev
Iv tried this now and it's not exactly the right result im looking for. This still only returns one "data" sample. I need every "data" sample that belongs to a given type to be added to that type. Iv edited the XML so that is easier to understand. Is there any way to edit this code to get the correct xml? Thx for taking the time!
Marthin
@Marthin: I've added another solution. Is that what you're looking for?
dtb
@dtb You rock my world! Thank you!
Marthin