开发者

Help with Linq to XML

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="D开发者_如何学编程ataTypes")]
    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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜