Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 加载然后读取xml?_C#_Xml_Serialization_Mvvm - Fatal编程技术网

C# 加载然后读取xml?

C# 加载然后读取xml?,c#,xml,serialization,mvvm,C#,Xml,Serialization,Mvvm,我正在写一个需要读取xml数据的程序,但我不知道如何读取 我知道如何将它绑定到xaml,但我不需要它。元素不是静态的,更改其值,其中一些元素在移动后显示。 我需要在mvvm模型中读入xml,因为它只是程序的一个元素 以下是xml的简短版本: <Stores> <Tank Name="Side fresh water tank No.1 SB" Weight="0.00" SG="1.000" VolumeMax="144.01"> <Depart

我正在写一个需要读取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>

我是用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 });
                    }
                }
            }


        }
    }

}

你的问题是什么?你是怎么解决的?您是否被卡住了?您可以使用类来读取XML
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 });
                    }
                }
            }


        }
    }

}