使用Linq查询在List <customclass>中解析XML

时间:2015-12-14 23:30:45

标签: c# xml linq

我的问题是如何实现从XML文件解析为List类的linq查询 这是自定义类:

CustomClass.cs

public class ListItem
{
    public int id;
    public string name;
    public int minLevel;
    public int minAttr1;
    public int minAttr2;
    public float damage;
    public float additionalDmg;
    public string additionalDmgType;
    public float range;
    public float cost;

    public ListItem(
        int _id,
        string _name,
        int _minLevel,
        int _minAttr1,
        int _minAttr2,
        float _damage,
        float _additionalDmg,
        string _additionalDmgType,
        float _range,
        float _cost)
    {
        id = _id;
        name = _name;
        minLevel = _minLevel;
        minAttr1 = _minAttr1;
        minAttr2 = _minAttr2;
        damage = _damage;
        additionalDmg = _additionalDmg;
        additionalDmgType = _additionalDmgType;
        range = _range;
        cost = _cost;
    }
}

这是我拥有的xml文件的一个示例:

File.xml

<?xml version="1.0" encoding="utf-8"?>
<Weapons>
  <Weapon id="0">
    <Name>spada</Name>
    <MinLevel>1</MinLevel>
    <MinAttr1>10</MinAttr1>
    <MinAttr2>5</MinAttr2>
    <Damage>100</Damage>
    <AdditionalDmg>10</AdditionalDmg>
    <AdditionalDmgType>Electric</AdditionalDmgType>
    <Range>1</Range>
    <Cost>1000</Cost>
  </Weapon>
  <Weapon id="1">
    <Name>spada2</Name>
    <MinLevel>1</MinLevel>
    <MinAttr1>10</MinAttr1>
    <MinAttr2>5</MinAttr2>
    <Damage>100</Damage>
    <AdditionalDmg>10</AdditionalDmg>
    <AdditionalDmgType>Electric</AdditionalDmgType>
    <Range>1</Range>
    <Cost>1000</Cost>
  </Weapon>
</Weapons>

这是需要linq查询进行解析的Importer类:

Importer.cs

class Importer
    {
        public void ImportXML()
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                System.IO.StreamReader sr = new System.IO.StreamReader(openFileDialog1.FileName);
                XDocument xml = XDocument.Load(sr);
                sr.Close();

                //Miss here linq query 
            }
        }
    }
}

我的尝试(但“数据”变量仍然为空):

 var data = from item in xml.Elements("weapon")
                       select new
                       {
                           name = item.Element("Name").Value,
                           minLevel = item.Element("MinLevel").Value
                       };
            foreach (var p in data)
            Debug.WriteLine(p.ToString());

2 个答案:

答案 0 :(得分:2)

四个问题:

  1. <Weapon>元素是<Weapons>根元素的子元素,而不是整个文档。

  2. XML区分大小写,因此在按名称搜索元素或属性时,您需要完全匹配案例。因此xml.Root.Elements("weapon")应为xml.Root.Elements("Weapon")

  3. XElement值转换为其他类型时,例如int,您应该使用explicit casting。这正确地处理国际化。并对XAttribute次转化执行相同的操作。

  4. “id”是一个属性,因此请使用XElement.Attribute()来访问它。

  5. 因此:

            var data = from item in xml.Root.Elements("Weapon")
                       select new
                       {
                           name = (string)item.Element("Name"),
                           minLevel = (int)item.Element("MinLevel"),
                           id = (int)item.Attribute("id")
                       };
    

答案 1 :(得分:1)

Elements子项只返回XContainer的直接子项,因此在您的情况下xml.Elements("weapon")会返回空集合,因为没有任何weapon元素XML的根源。

你应该使用

var data = from item in xml.Root.Elements("Weapon")
           select new {
               name = item.Element("Name").Value,
               minLevel = item.Element("MinLevel").Value
           };