A
A
anonymous2017-10-31 14:57:54
.NET
anonymous, 2017-10-31 14:57:54

How to correctly read from XML?

<dataset-data>
    <limit nil="true"/>
    <transform nil="true"/>
    <column-index nil="true"/>
    <column-names type="array">
      <column-name>Date</column-name>
      <column-name>Open</column-name>
      <column-name>High</column-name>
      <column-name>Low</column-name>
      <column-name>Close</column-name>
      <column-name>Volume</column-name>
      <column-name>Ex-Dividend</column-name>
      <column-name>Split Ratio</column-name>
      <column-name>Adj. Open</column-name>
      <column-name>Adj. High</column-name>
      <column-name>Adj. Low</column-name>
      <column-name>Adj. Close</column-name>
      <column-name>Adj. Volume</column-name>
    </column-names>
    <start-date type="date">1999-11-18</start-date>
    <end-date type="date">2017-10-30</end-date>
    <frequency>daily</frequency>
    <data type="array">
      <datum type="array">
        <datum type="date">2017-10-30</datum>
        <datum type="float">67.8</datum>
        <datum type="float">67.93</datum>
        <datum type="float">67.1</datum>
        <datum type="float">67.49</datum>
        <datum type="float">846605.0</datum>
        <datum type="float">0.0</datum>
        <datum type="float">1.0</datum>
        <datum type="float">67.8</datum>
        <datum type="float">67.93</datum>
        <datum type="float">67.1</datum>
        <datum type="float">67.49</datum>
        <datum type="float">846605.0</datum>
      </datum>
      <datum type="array">
        <datum type="date">2017-10-27</datum>
        <datum type="float">67.81</datum>
        <datum type="float">68.01</datum>
        <datum type="float">67.41</datum>
        <datum type="float">67.97</datum>
        <datum type="float">1440956.0</datum>
        <datum type="float">0.0</datum>
        <datum type="float">1.0</datum>
        <datum type="float">67.81</datum>
        <datum type="float">68.01</datum>
        <datum type="float">67.41</datum>
        <datum type="float">67.97</datum>
        <datum type="float">1440956.0</datum>
      </datum>

How to correctly count into an object after each where date and float are?
preferably via LINQ+XML

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Eremin, 2017-10-31
@EreminD

https://metanit.com/sharp/tutorial/16.6.php

C
cicatrix, 2017-10-31
@cicatrix

Something like this.

public class XmlEntry
{
    public DateTime Date { get; set; }
    public float[] Values { get; set; }
} // class XmlEntry

public IEnumerable<XmlEntry> ReadFile(string filename)
{
    XmlDocument doc = new XmlDocument();
    doc.Load(filename);
    return doc.SelectNodes("dataset/data/datum") // Здесь возможно подредактировать путь надо, у вас XML обрезан
        .OfType<XmlElement>()
        .Select(e => new XmlEntry
        {
            Date = DateTime.Parse(
                e.ChildNodes.OfType<XmlElement>()
                .Where(cn =>
                    cn.Attributes["type"].Value.Equals("date")).FirstOrDefault().InnerText),
            Values =
                e.ChildNodes.OfType<XmlElement>()
                .Where(cn =>
                    cn.Attributes["type"].Value.Equals("float"))
                    .Select(node => float.Parse(node.InnerText, System.Globalization.CultureInfo.InvariantCulture))
                    .ToArray()
        }); 
} // ReadFile

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question