2016-09-20 67 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