【问题标题】:C# SQLXML Deserialization to Class or ObjectC# SQLXML 反序列化为类或对象
【发布时间】:2016-09-20 05:01:15
【问题描述】:

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 移动版和桌面版的通用应用程序。但我无法弄清楚这是怎么回事。

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 Parsing 的了解还不够,无法找到下一步。

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

【问题讨论】:

  • 到目前为止你尝试过什么?您在实施解决方案时遇到了哪些问题?
  • 总是得到这个作为回复是什么意思,我已经尝试了简单的 XML 解串器我不再有我尝试过的代码,因为它没有做我需要的,所以我摆脱了它.我尝试过 stackoverflow.com/questions/10150785/using-xmltextreader 和其他 XML 反序列化技术,但由于这不是标准 XML,它们不会按照我希望的方式工作

标签: c# xml sqlxml


【解决方案1】:

尝试关注。不得不修改 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; }
    }
}

【讨论】:

  • 忽略这一点,我成功了,我在你的代码上方输入了一些东西。
  • 感谢您的帮助 :) 如果我在将它用于 API 的任何其他部分时遇到任何问题,我会在此处发布,我们会看到,但从您的代码看来,它会非常相似
猜你喜欢
  • 1970-01-01
  • 2020-07-19
  • 2022-01-21
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 2022-12-13
相关资源
最近更新 更多