Просто для удовольствия есть несколько интересных сайтов для преобразования вашего XML в классы C # (например, Xml2CSharp.com ). Используя этот инструмент, вы можете создать набор классов для десериализации и затем использовать LINQ to Objects.
Commodities commodities = null;
using (var stream = new StringReader(xmlString))
{
XmlSerializer serializer = new XmlSerializer(typeof(Commodities));
commodities = (Commodities) serializer.Deserialize(stream);
}
var commodityName = "corn";
var platformName = "bloomberg";
var year = "2018";
var commodity = commodities.Grains.Commodity.Single(c => c.Name.Equals(commodityName, StringComparison.InvariantCultureIgnoreCase));
var symbol = commodity.Specs.SymbolRoot.Platform.Single(p => p.Name.Equals(platformName, StringComparison.InvariantCultureIgnoreCase));
var months = commodity.ContractMonths.Month.Year.Where(y => y.Value.Equals(year, StringComparison.InvariantCultureIgnoreCase));
Вот классы, которые я создал с помощью инструмента. Это, конечно, намного больше кода, но мне нравится иметь конкретные классы для работы. Надеюсь, это полезно.
[XmlRoot(ElementName = "Platform")]
public class Platform
{
[XmlAttribute(AttributeName = "value")]
public string Value { get; set; }
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
}
[XmlRoot(ElementName = "SymbolRoot")]
public class SymbolRoot
{
[XmlElement(ElementName = "Platform")]
public List<Platform> Platform { get; set; }
}
[XmlRoot(ElementName = "Specs")]
public class Specs
{
[XmlElement(ElementName = "SymbolRoot")]
public SymbolRoot SymbolRoot { get; set; }
}
[XmlRoot(ElementName = "Year")]
public class Year
{
[XmlAttribute(AttributeName = "value")]
public string Value { get; set; }
[XmlAttribute(AttributeName = "firstnoticedate")]
public string Firstnoticedate { get; set; }
[XmlAttribute(AttributeName = "lasttradedate")]
public string Lasttradedate { get; set; }
[XmlAttribute(AttributeName = "dateformat")]
public string Dateformat { get; set; }
}
[XmlRoot(ElementName = "Month")]
public class Month
{
[XmlElement(ElementName = "Year")]
public List<Year> Year { get; set; }
[XmlAttribute(AttributeName = "value")]
public string Value { get; set; }
}
[XmlRoot(ElementName = "ContractMonths")]
public class ContractMonths
{
[XmlElement(ElementName = "Month")]
public Month Month { get; set; }
[XmlAttribute(AttributeName = "firstnoticedaterule")]
public string Firstnoticedaterule { get; set; }
[XmlAttribute(AttributeName = "lasttradedaterule")]
public string Lasttradedaterule { get; set; }
}
[XmlRoot(ElementName = "Commodity")]
public class Commodity
{
[XmlElement(ElementName = "Specs")]
public Specs Specs { get; set; }
[XmlElement(ElementName = "ContractMonths")]
public ContractMonths ContractMonths { get; set; }
[XmlAttribute(AttributeName = "title")]
public string Title { get; set; }
[XmlAttribute(AttributeName = "value")]
public string Value { get; set; }
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
}
[XmlRoot(ElementName = "Grains")]
public class Grains
{
[XmlElement(ElementName = "Commodity")]
public List<Commodity> Commodity { get; set; }
}
[XmlRoot(ElementName = "Commodities")]
public class Commodities
{
[XmlElement(ElementName = "Grains")]
public Grains Grains { get; set; }
}