通过linq反序列化xml,默认包含许多元素

时间:2014-11-24 18:58:19

标签: c# xml xml-serialization linq-to-xml deserialization

我有这个想要反序列化的xml,但问题是我无法在每天获得所有<food>个元素。是否有任何方法可以通过linq

对其进行反序列化
<foodplaces>
    <foodplace>
        <name> The Indian Restaurant</name>
        <week> 47 </week>
        <monday>
            <food> Pasta </food>
            <food> chineese food</food>
            <food> veg food </food>
        </monday>
        -<tuesday>
            <food> Indian food</food>
            <food> Veg food </food>
        </tuesday>
    </foodplace>

    <name> Restauran Italian </name>
    <week> 47 </week>
    -<monday>
        <food> Pizza </food>
        <food> Checken      </food>
        <food>  sallad </food>
    </monday>
    -<tuesday>
        <food> Fish </food>
        <food>  ris </food>
        <food> Biff </food>
        <food> Checken </food>
    </tuesday>
</foodplaces>

并拥有这些课程

public   class tuesday
{
    private string[] _foods = new string[3];
    public string food1
    {
        set { _foods[0] = value; }
    }
    public string food2
    {
        set { _foods[1] = value; }
    }
    public string food3
    {
        set { _foods[2] = value; }
    }
}

public class foodplace
{
    private string _name;
    private string _week;
    public string name
    {
        get { return _name; }
        set { _name = value; }
    }
    public string week
    {
        get { return _week; }
        set { _week = value; }
    }
    public monday m = new monday();
    public tuesday t = new tuesday();
}

并使用此代码将数据从XmlDocument反序列化到类中,但它似乎不起作用

foodplace fd = new foodplace();

List<foodplace> foodplaces =
    (from _foodplace in xdocument.Element("foodplaces").Elements("foodplace")
     select new foodplace
     {
         name = _foodplace.Element("name").Value,
         week = _foodplace.Element("week").Value,          
         m = (from _day in _foodplace.Elements("monday")
              select new tuesday 
              {                   
                  food1 = _day.Element("food").Value,
                  food2 = _day.Element("food").Value,                  
                  food3 = _day.Element("food").Value                      
              })

1 个答案:

答案 0 :(得分:0)

你有一些错误:

  1. “Restauran Italian”的XML未被<foodplace> </foodplace>括起来。它应该是。

  2. 您选择名为“星期一”的元素,将其数据存储在tuesday类中,然后将它们设置在m字段中,即monday。当然那些都应该是“星期一”。你有没有修复你的编译器错误?

  3. 有三个food元素,因此您必须通过它们进行枚举,类似于您对foodplace元素所做的操作。 _day.Element("food").Value(反复)得到第一个。

  4. 因此,以下是一个修复:

            string xml = @"
            <foodplaces>
                <foodplace>
                    <name> The Indian Restaurant</name>
                    <week> 47 </week>
                    <monday>
                        <food> Pasta </food>
                        <food> chineese food</food>
                        <food> veg food </food>
                    </monday>
                    <tuesday>
                        <food> Indian food</food>
                        <food> Veg food </food>
                    </tuesday>
                </foodplace>
                <foodplace>
                    <name> Restauran Italian </name>
                    <week> 47 </week>
                    <monday>
                        <food> Pizza </food>
                        <food> Checken      </food>
                        <food>  sallad </food>
                    </monday>
                    <tuesday>
                        <food> Fish </food>
                        <food>  ris </food>
                        <food> Biff </food>
                        <food> Checken </food>
                    </tuesday>
                </foodplace>
            </foodplaces>
            ";
    

            List<foodplace> foodplaces =
                (from _foodplace in xdocument.Element("foodplaces").Elements("foodplace")
                 select new foodplace
                 {
                     name = _foodplace.Element("name").Value,
                     week = _foodplace.Element("week").Value,
                     m = (from _day in _foodplace.Elements("monday")
                          let list = _day.Elements("food").Take(3).ToList()
                          select new monday
                          {
                              food1 = (list.Count > 0 ? list[0].Value : string.Empty),
                              food2 = (list.Count > 1 ? list[1].Value : string.Empty),
                              food3 = (list.Count > 2 ? list[2].Value : string.Empty),
                          }).FirstOrDefault(),
                 }).ToList();
    

    这很有效。

    顺便说一句,为mondaytuesday等单独的类似乎是错误的。根据需要创建基类RestaurantDay和子类。另外,我建议您查看c#的General Naming Conventions,您的代码可能更具可读性,例如公共字段m应该成为公共属性,如下所示:

     public Monday Monday { get; set; }