C# 显示具有相同节点值的XML数据项

C# 显示具有相同节点值的XML数据项,c#,xml,C#,Xml,好的,首先我完全知道这个问题,但它没有回答我的问题 好的,首先,这是我的xml <xml_api_reply version="1"> <weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0"> <forecast_conditions> <day_of_week data="Wed"/> <low da

好的,首先我完全知道这个问题,但它没有回答我的问题

好的,首先,这是我的xml

<xml_api_reply version="1">
  <weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">
  <forecast_conditions>
    <day_of_week data="Wed"/>
    <low data="57"/>
    <high data="68"/>
    <icon data="/ig/images/weather/partly_cloudy.gif"/>
    <condition data="Partly Cloudy"/>
  </forecast_conditions>
    <forecast_conditions>
      <day_of_week data="Thu"/>
      <low data="57"/>
      <high data="68"/>
      <icon data="/ig/images/weather/chance_of_rain.gif"/>
      <condition data="Chance of Rain"/>
    </forecast_conditions>
    <forecast_conditions>
      <day_of_week data="Fri"/>
      <low data="59"/>
      <high data="68"/>
      <icon data="/ig/images/weather/cloudy.gif"/>
      <condition data="Cloudy"/>
    </forecast_conditions>
    <forecast_conditions>
      <day_of_week data="Sat"/>
      <low data="59"/>
      <high data="68"/>
      <icon data="/ig/images/weather/cloudy.gif"/>
      <condition data="Cloudy"/>
    </forecast_conditions>
  </weather>
</xml_api_reply>
但是由于所有的航向节点都是相同的(
forecast\u conditions
),我对如何做到这一点有点困惑

到目前为止,我就是这样做到的

foreach (XmlNode node in xmlConditions.SelectNodes("/xml_api_reply/weather/forecast_conditions"))
{
    Conditions condition = new Conditions();
    condition.City = xmlConditions.SelectSingleNode("/xml_api_reply/weather/forecast_information/city").Attributes["data"].InnerText;
    condition.Condition = node.SelectSingleNode("condition").Attributes["data"].InnerText;
    condition.High = node.SelectSingleNode("high").Attributes["data"].InnerText;
    condition.Low = node.SelectSingleNode("low").Attributes["data"].InnerText;
    condition.DayOfWeek = node.SelectSingleNode("day_of_week").Attributes["data"].InnerText;
    conditions.Add(condition);
}
还有这个

public class Conditions
{
    public string DayOfWeek
    {
        get { return dayOfWeek; }
        set { dayOfWeek = value; }
    }

依此类推

如果将XML存储在
XDocument
对象中,则可以使用LINQ执行此操作。我还没有完全测试过,但请尝试以下方法:

XDocument doc = XDocument.Load(stream_of_your_xml);
var allConditions = from forecast_conditions in doc.Descendants("forecast_conditions") 
            select new Conditions()
            {
                DayOfWeek = forecast_conditions.Element("day_of_week").Attribute("data").Value,
                Low = forecast_conditions.Element("low").Attribute("data").Value
                //Do the same for your other values
            };
List<Conditions> theConditions = (List<Conditions>) allConditions;
XDocument doc=XDocument.Load(xml的流);
var allConditions=来自文档子体中的预测条件(“预测条件”)
选择新条件()
{
DayOfWeek=forecast_conditions.Element(“周中的天”).属性(“数据”).值,
低=预测条件。元素(“低”)。属性(“数据”)。值
//对其他值执行相同的操作
};
列出条件=(列出)所有条件;
XDocument doc = XDocument.Load(stream_of_your_xml);
var allConditions = from forecast_conditions in doc.Descendants("forecast_conditions") 
            select new Conditions()
            {
                DayOfWeek = forecast_conditions.Element("day_of_week").Attribute("data").Value,
                Low = forecast_conditions.Element("low").Attribute("data").Value
                //Do the same for your other values
            };
List<Conditions> theConditions = (List<Conditions>) allConditions;