简体   繁体   English

帮助Linq to XML

[英]Help with Linq to XML

Iv got two DB tables. iv有两个数据库表。 One containing types(Id, Name) and the other holds datapoints (RefId, Date, Value) that are referenced by the types. 一个包含类型(Id,名称),另一个包含类型所引用的数据点(RefId,日期,值)。 I need to create a XML file with the following strukture: 我需要使用以下结构创建一个XML文件:

<?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 iv获得了以下代码来执行此操作

    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. 我在使用LINQ和内部联接时遇到问题。 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)))));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM