将xml文件加载到自定义类的数组中

时间:2015-05-12 19:33:52

标签: c# xml

我有一个包含以下200个数据的xml文件

    <Dices>
      <Dice>
        <Sequence>1</Sequence>
        <Dice1>2</Dice1>
        <Dice2>2</Dice2>
      </Dice>
      <Dice>
        <Sequence>2</Sequence>
        <Dice1>3</Dice1>
        <Dice2>4</Dice2>
      </Dice>

在我的主程序中,我有一类“骰子”

int _sequence, _dice1, _dice2
public Dice(sequence, dice1, dice2) ......
public int sequence { get { return _sequence; } set { _sequence = value; } }
public int dice1 { get { return _dice1; } set { _dice1 = value; } }
public int dice2 { get { return _dice2; } set { _dice2 = value; } }

如何将xml加载到Dice类的数组中? 谢谢!

哎呀,我忘了包含自己的代码 我认为这会起作用,但显然它会为所有人返回null 请看看我?

Dice[] load_dices = (from dice in load_xml.Elements("Dice")
                                 select new Dice
                                            {
                                                sequence = (int)dice.Element("Sequence"), 
                                                dice1 = (int)dice.Element("Dice1"), 
                                                dice2 = (int)dice.Element("Dice2")
                                            })
                                .ToArray();

3 个答案:

答案 0 :(得分:1)

使用System.Xml.Serialization属性

using System.Xml.Serialization;
using System.Collections.Generic;

[XmlRoot("dices")]
public class DicesElement
{
    [XmlArray("dice")]
    [XmlArrayItem("dice", Type = typeof(DiceElement))]
    public List<DiceElement> Dice { get; set;}
}

...

using System.Xml.Serialization;

public class DiceElement
{
    [XmlElement("Sequence")]
    public string Sequence { get; set; }

    [XmlElement("Dice1")]
    public string Dice1{ get; set; }

    [XmlElement("Dice2")]
    public string Dice2{ get; set; }
}

然后你可以反序列化它们并完成繁重的工作。

有关反序列化的更多信息:https://blog.udemy.com/csharp-serialize-to-xml/

答案 1 :(得分:0)

您必须先选择根元素

var pindexed = $("input:radio[name='pindexed']:checked").val();

打印:

public class Program
{
    public static void Main(string[] args)
    {
        string xml = @"<Dices>
  <Dice>
    <Sequence>1</Sequence>
    <Dice1>2</Dice1>
    <Dice2>2</Dice2>
  </Dice>
  <Dice>
    <Sequence>2</Sequence>
    <Dice1>3</Dice1>
    <Dice2>4</Dice2>
  </Dice></Dices>";
        XDocument xd = XDocument.Parse(xml);
        Dice[] load_dices = (from dice in xd.Root.Elements("Dice")
                             select new Dice
                             {
                                 Sequence = (int)dice.Element("Sequence"),
                                 Dice1 = (int)dice.Element("Dice1"),
                                 Dice2 = (int)dice.Element("Dice2")
                             }).ToArray();
        foreach (var x in load_dices)
            Console.WriteLine(x);
    }
}
public class Dice
{
    public int Sequence { get; set; }
    public int Dice1 { get; set; }
    public int Dice2 { get; set; }

    public override string ToString()
    {
        return string.Format("{0}\t{1}\t{2}", Sequence, Dice1, Dice2);
    }
}

链接:http://rextester.com/IFABI55474

答案 2 :(得分:0)

另一个可能有用的部分是您在尝试查找项目时必须使用默认命名空间。

var xDoc = XDocument.Load(fileInfo.FullName, LoadOptions.PreserveWhitespace);
    var defaultNs = xDoc.Root.GetDefaultNamespace();        
    var packageElement = xDoc.Descendants(defaultNs + "package")                                
                                .Where(n => string.IsNullOrWhiteSpace(n.Attribute("id").Value) == false
                                            && n.Attribute("id").Value.Equals("SomeValue", StringComparison.CurrentCultureIgnoreCase)
                                            && n.Attribute("version").Value.Equals("1.1.2") == false)
                                .SingleOrDefault();

如果不向Descendants方法添加默认命名空间值,它将找不到任何内容。我不确定这是否是您的问题,但我也想为您提供这些信息。