2016-09-20 87 views
-1

http://rtt.metroinfo.org.nz/RTT/Public/Utility/File.aspx?ContentType=SQLXML&Name=JPPlatform.xmlC#SQLXML反序列化类,对象

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

+2

你到目前为止尝试过什么?您在实施解决方案时遇到了哪些问题? –

+0

这是怎么回事,我已经尝试了简单的XML deserlizers我不再有我的代码,我试过,因为它没有做我所需要的,所以我摆脱了它。我已经尝试了像http://stackoverflow.com/questions/10150785/using-xmltextreader和其他XML deserlization技术的东西,但由于这不是标准的XML他们不会按照我想要的方式工作 – Jalomba

回答

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; } 
    } 
} 
+0

忽视,我得到它的工作我想念你的代码上面键入的东西。 – Jalomba

+0

感谢您的帮助:)如果我有任何问题将其用于API的任何其他部分,我会在这里发布,我们将会看到,但是从您的代码看起来它会非常相似 – Jalomba