【问题标题】:Load and then read xml? [duplicate]加载然后读取xml? [复制]
【发布时间】:2019-01-28 10:34:34
【问题描述】:

我正在编写一个需要读取 xml 数据的程序,但我不明白该怎么做。

我知道如何将它绑定到 xaml,但我不需要。元素不是静态的,它们会改变它们的值,其中一些会在一些动作之后显示出来。 我需要在 mvvm 模型中读取 xml,因为它只是程序的一个元素。

这是 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>

接下来的想法。 没有按钮。只有两个数据网格。 首先有 4 列,其中名称、重量、SG、体积。 重量和体积为 0,重量 = sg * 体积,体积 = 重量 / sg。 在更改三个(sg 太)值之一后检查(音量!= 0),如果是: 在第二个数据网格上显示名称、volume、x、y、z、volumex、volumey、volume*z。

如果我使用这样的东西,我的下一步是什么?

 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; }

    }
}

【问题讨论】:

  • 你的问题是什么?你试图解决什么? 特别你被卡住了吗?
  • 可以使用XmlDocument类进行XML读取

标签: c# xml serialization mvvm


【解决方案1】:

我用 xml linq 做到了。我创建了两个数据表。您可以将数据表作为 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 });
                    }
                }
            }


        }
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-04
    • 1970-01-01
    • 1970-01-01
    • 2012-05-19
    • 2016-07-23
    • 2015-05-19
    • 2019-06-18
    • 1970-01-01
    相关资源
    最近更新 更多