简体   繁体   中英

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="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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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