-1

I'm writing a programm which need to read xml data, but i can't understand how to do it.

I know how to bind it to xaml but i don't need that. Elements not static, change their values and some of them are showing after some moves. And i need readin the xml in mvvm model because it's only one element of the programm.

Here's the short ver of xml:

<Stores>
    <Tank Name="Side fresh water tank No.1 SB" Weight="0.00" SG="1.000" VolumeMax="144.01">
      <DepartureTable>
      <Volume Level="0.00" Value="0.00" X="-29.30" Y="8.10" Z="1.30" SFS="0.00" SFX="0.00" SFY="0.00" SFIX="0.00" SFIY="0.00"/>
      <Volume Level="0.10" Value="1.35" X="-29.65" Y="8.07" Z="1.35" SFS="13.90" SFX="-29.50" SFY="8.10" SFIX="8.30" SFIY="378.00"/>
      <Volume Level="0.20" Value="2.78" X="-29.71" Y="8.07" Z="1.40" SFS="14.60" SFX="-29.70" SFY="8.10" SFIX="8.70" SFIY="396.00"/>
      <Volume Level="0.30" Value="4.28" X="-29.77" Y="8.07" Z="1.45" SFS="15.30" SFX="-29.80" SFY="8.10" SFIX="9.10" SFIY="413.00"/>
    </Tank>
</Stores>

The idea next. No buttons. Only two datagrids. first have 4 columns where name, weight, sg, volume. weight and volume are 0 and weight = sg * volume, volume = weight / sg. after changing one of three (sg too) values check (volume != 0) and if yes: Show on the secind datagrid name, volume, x, y, z, volumex, volumey, volume*z.

if i use something like this what is my next step?

 public class StoresModel
{
    private StoresModel()
    {
        XmlDocument xml = new XmlDocument();

        xml.Load("Tanks.xml");
    }


        [XmlRoot("Stores")]
    public class StoresCollection
    {
        [XmlElement("Tank")]
        public Tank[] Tanks { get; set; }
    }

    public class Tank
    {
        [XmlAttribute("Name")]
        public string Name { get; set; }

        [XmlAttribute("Weight")]
        public string Weight { get; set; }

        [XmlAttribute("SG")]
        public string SG { get; set; }

        [XmlAttribute("VolumeMax")]
        public string VolumeMax { get; set; }

        [XmlArray("DepartureTable")]
        [XmlArrayItem("Volume", typeof(DepartureVolume))]
        public DepartureVolume[] Volumes { get; set; }

    }

    public class DepartureVolume
    {
        [XmlAttribute("Level")]
        public double Level { get; set; }

        [XmlAttribute("Value")]
        public double Value { get; set; }

        [XmlAttribute("X")]
        public double X { get; set; }

        [XmlAttribute("Y")]
        public double Y { get; set; }

        [XmlAttribute("Z")]
        public double Z { get; set; }

        [XmlAttribute("SFS")]
        public double SFS { get; set; }

        [XmlAttribute("SFX")]
        public double SFX { get; set; }

        [XmlAttribute("SFY")]
        public double SFY { get; set; }

        [XmlAttribute("SFIX")]
        public double SFIX { get; set; }

        [XmlAttribute("SFIY")]
        public double SGIY { get; set; }

    }
}
Lee Shane
  • 1
  • 2

1 Answers1

-1

I did it with xml linq. I create two DataTables. You can make the datatables the datasource of the DGV.

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

namespace ConsoleApplication98
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            DataTable dt1 = new DataTable();
            dt1.Columns.Add("name", typeof(string));
            dt1.Columns.Add("weight", typeof(decimal));
            dt1.Columns.Add("sg", typeof(decimal));
            dt1.Columns.Add("volume", typeof(decimal));

            DataTable dt2 = new DataTable();
            dt2.Columns.Add("name", typeof(string));
            dt2.Columns.Add("volume", typeof(decimal));
            dt2.Columns.Add("x", typeof(decimal));
            dt2.Columns.Add("y", typeof(decimal));
            dt2.Columns.Add("z", typeof(decimal));

            XDocument doc = XDocument.Load(FILENAME);

            foreach (XElement tank in doc.Descendants("Tank"))
            {
                string name = (string)tank.Attribute("Name");
                decimal weight = (decimal)tank.Attribute("Weight");
                decimal sg = (decimal)tank.Attribute("SG");
                decimal volumnMax = (decimal)tank.Attribute("VolumeMax");

                dt1.Rows.Add(new object[] { name, weight, sg, volumnMax });

                foreach (XElement volume in tank.Descendants("Volume"))
                {
                    decimal value = (decimal)volume.Attribute("Value");
                    if (value != (decimal)0.00)
                    {
                        decimal x = (decimal)volume.Attribute("X");
                        decimal y = (decimal)volume.Attribute("Y");
                        decimal z = (decimal)volume.Attribute("Z");

                        dt2.Rows.Add(new object[] { name, value, x, y, z });
                    }
                }
            }


        }
    }

}
jdweng
  • 33,250
  • 2
  • 15
  • 20