![](/img/trans.png)
[英]Delete an element with specific node name from XML file using XDocument and XmlDocument class
[英]Xdocument getting element node value from another note in the xml file
我正在嘗試從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++;
}
有什么建議么?
您只是在會話和電影之間進行聯接。 只要這樣做:
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,
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.