C# 使用C读取XML文件属性和元素

C# 使用C读取XML文件属性和元素,c#,xml,C#,Xml,我的XML文件如下所示: <device name="dev. 1" nodes="16" scenarios="20"> <package>Pack. 1</package> <info>info...</info> <picture>pic</picture> <nodes> <node no="1

我的XML文件如下所示:

<device name="dev. 1" nodes="16" scenarios="20">
  <package>Pack. 1</package>
  <info>info...</info>
  <picture>pic</picture>
  <nodes>
    <node no="1" name="OUT_1" type="source"/>
    <node no="2" name="OUT_2" type="source"/>
    <node no="3" name="OUT_3" type="source"/>
    <node no="4" name="OUT_4" type="source"/>
  </nodes>
  <scenario name="scenario 1">
    <zth m_node="1" d_node="1" model="table" points="190">
      <data time="1" value="0.1"/>
      <data time="2" value="2"/>
      <data time="2" value="4"/>
    </zth>
    <zth m_node="1" d_node="2" model="table" points="190">
      <data time="1" value="0.3"/>
      <data time="2" value="4"/>
    </zth>
  </scenario>
  <scenario name="scenario 2">
    <zth m_node="1" d_node="1" model="table" points="190">
      <data time="2" value="2"/>
      <data time="1" value="0.3"/>
      <data time="2" value="4"/>
    </zth>
    <zth m_node="1" d_node="2" model="table" points="190">
      <data time="1" value="0.3"/>
      <data time="2" value="4"/>
    </zth>
  </scenario>
</device>
我正在获取错误:严重性代码描述项目文件行抑制状态错误CS0029无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.List”


时间和值的数据也是double类型,我将其更改为string,因为我遇到了类似的错误,但它仍然不起作用。我做了一系列更改。除非有必要,我总是建议避免var。您完全困惑了,因为您不知道变量类型:

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

namespace ConsoleApplication16
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XMLReader xReader = new XMLReader();
            xReader.xmlReader(FILENAME);
        }
 
    }
    public class Scenario
    {
        public string name { get; set; }
        public List<Zth> ZthList { get; set; }
    }

    public class Zth
    {
        public string m_node { get; set; }
        public string d_node { get; set; }
        public string time { get; set; }
        public string value { get; set; }
    }
    public class XMLReader
    {

        public void xmlReader(string filename)
        {


            XDocument doc = XDocument.Load(filename);
            List<Scenario> scenarios = (from s in doc.Root.Elements("scenario")
                             select new Scenario
                             {
                                 name = (string)s.Attribute("name"),
                                 ZthList = s.Elements("zth")
                                            .Select(r => new Zth()
                                            {
                                                m_node = (string)r.Attribute("m_node"),
                                                d_node = (string)r.Attribute("d_node"),
                                                time = (string)r.Element("data").Attribute("time"),
                                                value = (string)r.Element("data").Attribute("value")
                                            }).ToList()
                             }).ToList();
            List<Scenario> zth_d_node = scenarios.Where(x => x.ZthList.Any(r => r.d_node == "1")).ToList();
            List<Scenario> s_names = scenarios.Where(x => x.name == "name").ToList();

            Console.WriteLine("List: ");
            Console.WriteLine(String.Join(", ", scenarios.Select(x => x.name )));
        }
    }
}

您需要改进模型以适应xml数据的结构。见:

public class Scenario {
    public string name { get; set; }
    public List<Zth> ZthList { get; set; }
}

public class Zth {
    public string m_node { get; set; }
    public string d_node { get; set; }
    public List<Data> data { get; set; }
}

public class Data {
    public string t { get; set; }
    public string v { get; set; }
}
然后结果将如下所示:


谢谢你的提示。我试图创建一个包含所有时间值的列表,但只有当zth元素中的d_节点属性为1时,我才能将其保存到另一个列表中?@kristian,如果d_节点属性不等于1怎么办?zth类的数据成员将为空!可以吗?如果d_节点不是1,则可以忽略时间和值,只有当它是1时才相关。当我显示t:Console.WriteLineString.Join的值时,scenarios.Selectx=>x.ZthList.Selecty=>y.data.Selectz=>z.t;我没有得到值,但得到的是:System.Linq.Enumerable+WhereSelectListIterator 2[prj.utils.Zth,System.Collections.Generic.IEnumerable 1[System.String]],System.Linq.Enumerable+WhereSelectListIterator 2[prj.utils.Zth,System.Collections.Generic.IEnumerable 1[System.String]]。我尝试以这种方式获取场景的名称,结果是:Console.WriteLineString.Join,scenarios.Selectx=>x.name;我做错什么了吗?foreachScenario sc在scenarios{Console.WriteLine$--==={sc.name}===--;foreachZth z在sc.ZthList{Console.WriteLinetimes:{0},string.Join;,z.data.Selectx=>x.t;Console.WriteLineValue:{0},string.Join;,z.data.Selectx=>x.v;}在这一行中,列出zth_d_node=场景。其中x=>x.ZthList.Anyr=>r.d_node==1.ToList;我试图创建另一个列表,在其中过滤zth元素并检查d_节点是否为一个,如果是这种情况,我希望保存元素数据的时间值。如何将这些值保存在列表中,我尝试的代码只显示真值,而不显示实际数据。您将看到WHERE的结果,该结果为真或假。您需要在返回数据的位置之后进行选择。您可以更具体一点吗。在这种情况下,如何从数据中选择时间属性?它已经是这些了。我给我的答案加了一张图片,这样你就可以看到了。
public class Scenario {
    public string name { get; set; }
    public List<Zth> ZthList { get; set; }
}

public class Zth {
    public string m_node { get; set; }
    public string d_node { get; set; }
    public List<Data> data { get; set; }
}

public class Data {
    public string t { get; set; }
    public string v { get; set; }
}
XDocument xdoc = XDocument.Load(...);
List<Scenario> scenarios = xdoc.Descendants("scenario")
    .Select(x=> new Scenario()
        {
            name= x.Attribute("name").Value, 
            ZthList= x.Descendants("zth")
                .Select(y=> new Zth()
                {
                    m_node = y.Attribute("m_node").Value,
                    d_node = y.Attribute("d_node").Value, 
                    data = y.Descendants("data")
                        .Select(z => new Data()
                        {
                            t =  z.Attribute("time").Value,
enter code here

                            v = z.Attribute("value").Value,
                        })
                        .ToList()
                }).ToList()
        }).ToList();
...
ZthList= x.Descendants("zth")
    .Where(n=> n.Attribute("d_node").Value == "1")  //condition was added
    .Select(y=> new Zth()
...