C#SQLXML反序列化为类或对象

时间:2016-09-20 05:01:16

标签: c# xml sqlxml

http://rtt.metroinfo.org.nz/RTT/Public/Utility/File.aspx?ContentType=SQLXML&Name=JPPlatform.xml

http://rtt.metroinfo.org.nz/RTT/Public/Utility/File.aspx?Name=JPRoutePositionET.xml&ContentType=SQLXML&PlatformTag=536

这两个都是我使用HttpClient提取的样本数据,我想获取PlatformTagETA之类的属性(?)等。

这是使用适用于Windows 10移动版和桌面版的Univeral Apps。但我无法弄清楚要做什么。

XDocument Document =  XDocument.Parse(RESPONSE_CONSTANT);
var Stops = from Stop in Document.Descendants("Platform")
select new
{
Platformtag = (string)Stop.Attribute("PlatformTag"),
Platformno = (string)Stop.Attribute("PlatformNo")};
foreach (var item in Stops)
BusData.Text = item.Platformtag;
}

我目前拥有的是什么,但没有任何东西来自它,它只是坐在那里,因为它什么都看不到,从这里我不太了解XML解析以找到下一步。

注意:Response_Constant包含如下数据:http://rtt.metroinfo.org.nz/RTT/Public/Utility/File.aspx?Name=JPRoutePositionET.xml&ContentType=SQLXML&PlatformTag=536

1 个答案:

答案 0 :(得分:0)

试试以下内容。不得不修改xml来替换&符号。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace ConsoleApplication14
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            string xml = File.ReadAllText(FILENAME);
            xml = xml.Replace("&", "&");
            StringReader sReader = new StringReader(xml);
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.ConformanceLevel = ConformanceLevel.Fragment;
            XmlReader reader = XmlReader.Create(sReader);

            List<Platform> platforms = new List<Platform>();
            while (!reader.EOF)
            {
                if (reader.Name != "Platform")
                {
                    reader.ReadToFollowing("Platform");
                }
                if (!reader.EOF)
                {
                    XElement platform = (XElement)XElement.ReadFrom(reader);

                    platforms.Add(new Platform()
                    {
                        tag = (int)platform.Attribute("PlatformTag"),
                        no = (int?)platform.Attribute("PlatformNo"),
                        name = (string)platform.Attribute("Name"),
                        bearingToRoad = (double?)platform.Attribute("BearingToRoad"),
                        roadName = (string)platform.Attribute("RoadName"),
                        lat = (double)platform.Element(platform.Name.Namespace + "Position").Attribute("Lat"),
                        _long = (double)platform.Element(platform.Name.Namespace + "Position").Attribute("Long")
                    });
                }
            }
        }
    }
    public class Platform
    {
        public int tag { get; set; }
        public int? no { get; set; }
        public string name { get; set; }
        public double? bearingToRoad { get; set; }
        public string roadName { get; set; }
        public double lat { get; set; }
        public double _long { get; set; }
    }
}