简体   繁体   中英

XDocument extracting XML data with the same elements but different attributes

So I have an XML Document that is structured sort of like this:

<product version="1.7">
        <forecast>
                <area type="location">
                        <forecast-period index="0">
                                <element type="air_temperate_minimum" units="Celsius">5</element>
                                <element type="air_temperate_maximum" units="Celsius">10</element>
                                <text type="forecast">Lorem ipsum dolor sit amet</text>
                                <text type="probability_of_precipitation">5%</text>
                        </forecast-period>
                        <forecast-period index="1">
                                <element type="air_temperate_minimum" units="Celsius">10</element>
                                <element type="air_temperate_maximum" units="Celsius">15</element>
                                <text type="forecast">Lorem ipsum dolor sit amet</text>
                                <text type="probability_of_precipitation">10%</text>
                        </forecast-period>
                        <forecast-period index="2">
                                <element type="air_temperate_minimum" units="Celsius">15</element>
                                <element type="air_temperate_maximum" units="Celsius">20</element>
                                <text type="forecast">Lorem ipsum dolor sit amet</text>
                                <text type="probability_of_precipitation">15%</text>
                        </forecast-period>
                        <forecast-period index="3">
                                <element type="air_temperate_minimum" units="Celsius">20</element>
                                <element type="air_temperate_maximum" units="Celsius">25</element>
                                <text type="forecast">Lorem ipsum dolor sit amet</text>
                                <text type="probability_of_precipitation">20%</text>
                        </forecast-period>
                </area>
        </forecast>
</product>

This was the code that I was trying to use:

XmlUrlResolver res = new XmlUrlResolver();
XmlReaderSettings set = new XmlReaderSettings();
set.XmlResolver = res;
XDocument doc = XDocument.Load(XmlReader.Create("ftp://ftp.website/file.xml", set));

IEnumerable<XElement> forecast = doc.Descendants("forecast-period");
foreach (XElement element in forecast)
{
    Console.WriteLine(element.Value);
}

The thing that I am trying to do is something like

Console.WriteLine(Index1TemperatureMinimum);

and returning 10

or

Console.WriteLine(Index3ProbabilityOfPrecipitation);

and returning 20%

thanks

See following:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            List<XElement> locations = doc.Descendants("area").Where(x => (string)x.Attribute("type") == "location").ToList();

            List<Forecast> forecasts = locations.SelectMany(x => x.Descendants("forecast-period").Select(y => new Forecast()
            {
                index = (int)y.Attribute("index"),
                minTemp = y.Elements("element").Where(z => (string)z.Attribute("type") == "air_temperate_minimum").Select(z => (int?)z).FirstOrDefault(),
                maxTemp = y.Elements("element").Where(z => (string)z.Attribute("type") == "air_temperate_maximum").Select(z => (int?)z).FirstOrDefault(),
                forecast = y.Elements("text").Where(z => (string)z.Attribute("type") == "forecast").Select(z => (string)z).FirstOrDefault(),
                precipitation = y.Elements("text").Where(z => (string)z.Attribute("type") == "probability_of_precipitation").Select(z => (x == null) ? null : (int?)int.Parse(((string)z).Substring(0, ((string)z).Length - 1))).FirstOrDefault()
            })).ToList();
        }
    }
    public class Forecast
    {
        public int? index { get; set; }
        public int? minTemp { get; set; }
        public int? maxTemp { get; set; }
        public string forecast { get; set; }
        public int? precipitation { get; set; }

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM