0

I made a previous post Parsing XML from string C# in which I parsed an array of elements from an XML string just below the root element. This time, I want to do nested parsing. I want to parse request which is under root/schedule/request from an XML string.

This is the XML string:

`<?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>`

I am getting a null response from my current code.

I designate the schedule class as the root node here and create a list of Request

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

I then create a list of Trip inside Request

public class Request
{
    [XmlArray("request"), XmlArrayItem("trip")]
    public Trip[] trips {get; set;} 
}

Inside trip is where I have all the elements that I want to use:

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

When I look at what happens in the debugger, trips is null. How do I successfully parse it to get all the elements?

Community
  • 1
  • 1
jipot
  • 304
  • 3
  • 13
  • 34

1 Answers1

1

Try code below. The tag names are case sensitive. I changed then Xml.... names as required.

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

}
​
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • This almost works! This fixed the problem of getting trips as null The only problem is that the "fare" is still null. Any idea? – jipot Jul 30 '15 at 02:55
  • Make sure to change "fare" to an XmlAttribute and NOT an XmlElement – jipot Jul 30 '15 at 03:02
  • With test data you provided it is working. Make sure your real data matches the test data. Usually issues like this is due to uppercase/lowercase tag names. – jdweng Jul 30 '15 at 06:53