嵌套的XML字符串解析c#

本文关键字:字符串 XML 嵌套 | 更新日期: 2023-09-27 18:05:23

我在之前的一篇文章中从字符串c#解析XML,其中我从root元素下方的XML字符串解析元素数组。这一次,我想做嵌套解析。我想从一个XML字符串解析root/schedule/request下的request

XML字符串:

`<?xml version="1.0" encoding="utf-8"?><root><uri><![CDATA[http://api.bart.gov/api/sched.aspx?cmd=depart&orig=DBRK&dest=EMBR]]></uri><origin>DBRK</origin><destination>EMBR</destination><sched_num>35</sched_num><schedule><date>Jul 29, 2015</date><time>5:25 PM</time><before>2</before><after>2</after><request><trip origin="DBRK" destination="EMBR" fare="3.90" origTimeMin="5:10 PM" origTimeDate="07/29/2015 " destTimeMin="5:32 PM" destTimeDate="07/29/2015" clipper="1.45"><leg order="1" transfercode="" origin="DBRK" destination="EMBR" origTimeMin="5:10 PM" origTimeDate="07/29/2015" destTimeMin="5:32 PM" destTimeDate="07/29/2015" line="ROUTE 7" bikeflag="1" trainHeadStation="MLBR" trainIdx="52"/></trip><trip origin="DBRK" destination="EMBR" fare="3.90" origTimeMin="5:18 PM" origTimeDate="07/29/2015 " destTimeMin="5:40 PM" destTimeDate="07/29/2015" clipper="1.45"><leg order="1" transfercode="S" origin="DBRK" destination="MCAR" origTimeMin="5:18 PM" origTimeDate="07/29/2015" destTimeMin="5:23 PM" destTimeDate="07/29/2015" line="ROUTE 4" bikeflag="1" trainHeadStation="FRMT" trainIdx="52"/><leg order="2" transfercode="" origin="MCAR" destination="EMBR" origTimeMin="5:23 PM" origTimeDate="07/29/2015" destTimeMin="5:40 PM" destTimeDate="07/29/2015" line="ROUTE 1" bikeflag="1" trainHeadStation="SFIA" trainIdx="65"/></trip><trip origin="DBRK" destination="EMBR" fare="3.90" origTimeMin="5:27 PM" origTimeDate="07/29/2015 " destTimeMin="5:49 PM" destTimeDate="07/29/2015" clipper="1.45"><leg order="1" transfercode="" origin="DBRK" destination="EMBR" origTimeMin="5:27 PM" origTimeDate="07/29/2015" destTimeMin="5:49 PM" destTimeDate="07/29/2015" line="ROUTE 7" bikeflag="1" trainHeadStation="MLBR" trainIdx="53"/></trip><trip origin="DBRK" destination="EMBR" fare="3.90" origTimeMin="5:33 PM" origTimeDate="07/29/2015 " destTimeMin="5:55 PM" destTimeDate="07/29/2015" clipper="1.45"><leg order="1" transfercode="S" origin="DBRK" destination="MCAR" origTimeMin="5:33 PM" origTimeDate="07/29/2015" destTimeMin="5:38 PM" destTimeDate="07/29/2015" line="ROUTE 4" bikeflag="1" trainHeadStation="FRMT" trainIdx="53"/><leg order="2" transfercode="" origin="MCAR" destination="EMBR" origTimeMin="5:38 PM" origTimeDate="07/29/2015" destTimeMin="5:55 PM" destTimeDate="07/29/2015" line="ROUTE 1" bikeflag="1" trainHeadStation="SFIA" trainIdx="67"/></trip></request></schedule><message><co2_emissions><![CDATA[<p>CO<sub>2</sub> emissions saved by this BART trip: <strong>9.2 pounds.</strong> <a href="http://www.bart.gov/guide/carbon">Read more</a></p>]]></co2_emissions></message></root>`

我从我当前的代码得到一个空响应。

我将schedule类指定为这里的根节点,并创建一个Request

列表
[XmlRoot("root")]
public class Schedule
{
    [XmlArray("schedule"), XmlArrayItem("request")]
    public Request[] requests {get; set;}
}

然后在Request

中创建一个Trip列表
public class Request
{
    [XmlArray("request"), XmlArrayItem("trip")]
    public Trip[] trips {get; set;} 
}

Inside trip是我想要使用的所有元素的地方:

public class Trip
{
    [XmlElement("fare")]
    public string fare  { get; set; }
}

当我查看调试器中发生的事情时,trips为空。我如何成功地解析它以获得所有元素?

嵌套的XML字符串解析c#

试试下面的代码。标记名区分大小写。我改变了Xml....

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:'temp'test.xml";
        static void Main(string[] args)
        {
            XmlSerializer xs = new XmlSerializer(typeof(Root));
            XmlTextReader reader = new XmlTextReader(FILENAME);
            Root root = (Root)xs.Deserialize(reader);
        }
    }
    [XmlRoot("root")]
    public class Root
    {
        [XmlElement("schedule")]
        public Schedule schedule {get; set;}
    }
    [XmlRoot("schedule")]
    public class Schedule
    {
        [XmlElement("request")]
        public Request[] requests {get; set;}
    }
    [XmlRoot("request")]
    public class Request
    {
        [XmlElement("trip")]
        public List<Trip> trips {get; set;} 
    }
    [XmlRoot("trip")]
    public class Trip
    {
        [XmlAttribute("fare")]
        public string fare  { get; set; }
    }
}
​