2 回答
TA贡献1816条经验 获得超6个赞
在这种情况下我不会使用序列化,因为它需要我的类然后其他方法。请参阅下面的 xml linq 解决方案:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication110
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
List<Key> keys = doc.Descendants("Key").Select(x => new Key()
{
Name = (string)x.Element("Name"),
Values = x.Descendants("KeyValue").Select(y => new KeyValue() {
Value = (string)y,
Offered = (bool)y.Attribute("Offered"),
Order = (int?)y.Attribute("Order")
}).ToList()
}).ToList();
}
}
public class Key
{
public string Name { get; set; }
public List<KeyValue> Values { get;set;}
}
public class KeyValue
{
public string Value { get; set; }
public int? Order { get; set; }
public bool Offered { get; set; }
}
}
- 2 回答
- 0 关注
- 61 浏览
添加回答
举报