Search code examples
c#xmlsqlxml

C# SQLXML Deserialization to Class or Object


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

Both of those are my sample data which I am pulling down using HttpClient, I want to grab the attributes(?) such as PlatformTag, ETA and the like.

This is using Univeral Apps for Windows 10 mobile and Desktop. But I can't figure out what's going on to do it.

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

Is what I currently have, but nothing comes from it, it just sits there like it sees nothing, from here I don't know enough about XML Parsing to find a next step.

Note: Response_Constant contains data like this: http://rtt.metroinfo.org.nz/RTT/Public/Utility/File.aspx?Name=JPRoutePositionET.xml&ContentType=SQLXML&PlatformTag=536


Solution

  • Try following. Had to modify xml to replace ampersand.

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