Xdocument从xml文件中的另一个注释获取元素节点值

时间:2016-06-07 06:22:41

标签: c# asp.net xml linq-to-xml

我试图从XML文件中获取一些数据 - 见下文。

基本上对于每个会话数据,我获取其中的所有元素并存储它,但我需要从电影引用的元素中获取movie_Name。

<Schedule_Data>
    <Movies>
        <Movie>
            <Cinema_ID>3169101</Cinema_ID>
            <Movie_ID>1012689</Movie_ID>
            <Movie_Name>2D Captain America: Civil War</Movie_Name>
            <Rating>PG13</Rating>
            <Runtime>160</Runtime>
        </Movie>
        <Movie>
            <Cinema_ID>3169101</Cinema_ID>
            <Movie_ID>1012984</Movie_ID>
            <Movie_Name>2D Zootopia</Movie_Name>
            <Rating>PG</Rating>
            <Runtime>115</Runtime>
        </Movie>
    </Movies>
    <Sessions>
        <Session>
            <Cinema_ID>8888888</Cinema_ID>
            <Movie_ID>1012689</Movie_ID>
            <Session_ID>1083592422</Session_ID>
            <Price_group_code>10007</Price_group_code>
            <Auditorium_Number>9</Auditorium_Number>
            <Assigned_Seating>True</Assigned_Seating>
            <Attribute></Attribute>
            <Date_time>20160607141000</Date_time>
            <Total_Seats>87</Total_Seats>
            <Available_Seats>87</Available_Seats>
        </Session>
        <Session>
            <Cinema_ID>8888888</Cinema_ID>
            <Movie_ID>1012984</Movie_ID>
            <Session_ID>1083592423</Session_ID>
            <Price_group_code>10007</Price_group_code>
            <Auditorium_Number>9</Auditorium_Number>
        </Session>
    </Sessions>
</Schedule_Data>

目前我的代码是:

XDocument thisXML = XDocument.Parse(responseText);

//get the dates element (which contains all the date nodes)
XElement datesElement = thisXML.Element("Schedule_Data").Element("Sessions");

//use linq to compile an ienumerable of the date nodes
var dates = from dateNode in datesElement.Elements("Session")
select dateNode;

//get the dates element (which contains all the film nodes)
XElement MoviesElement = thisXML.Element("Schedule_Data").Element("Movies");

foreach (XElement session in dates)
{
    //get movie name
    var films = from filmnode in MoviesElement.Elements("Movie")
    select filmnode;

    var movieId = session.Element("Movie_ID").Value;

    // This is where i try do the where clause and try get the value but it returns null
    var answer = from reply in films
    where reply.Element("Movie_ID").Value == movieId
    select films.Elements("Movie_Name");

    //create a new session import record
    ORM.Sessionimportattemptimportedsession newSessionimportattemptimportedsession = new ORM.Sessionimportattemptimportedsession();

    //check data and set properties
    newSessionimportattemptimportedsession.MovieTitle = answer.ToString();
    newSessionimportattemptimportedsession.ScreenNumber = session.Element("Screen_bytNum").Value;
    .....
    numberOfSessions++; 
}

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

您只是在会话和电影之间进行联接。就这样做:

var query =
    from s in doc.Descendants("Session")
    join m in doc.Descendants("Movie")
    on (string)s.Element("Movie_ID") equals (string)m.Element("Movie_ID")
    select new
    {
        MovieName = (string)m.Element("Movie_Name"),
        Session = s,
        Movie = m,
    };