您的位置:首页 > 其它

Linq to Xml 小结

2012-07-14 09:59 225 查看

Linq to xml 这个东西出来好多年了,但一直没有机会在项目中用到,前段时间,终于项目中一些地方需要用到xml作为数据源,于是就体验了一把这个,感觉还挺不错的,今天在此小结一下.


首先我们来模拟一下一个真实的业务场景:在这里我们需要展示一个火车站上各个站台上面灯光开关状态.假设每个站台一共6个开关,站台我们用Station节点表示,站台开关我们用SwitchNo来表示,在初始化(即默认状态)的时候,我们需要表示成第一个站台2个开关开着,第二个站台3个开关开着,第三个站台4个开关开着….

那么我们可能需要的一个xml文件格式可能就是如下这个样子:



<?xml version="1.0" encoding="utf-8"?>
<Stations>
<Station Id="1">
<SwitchNo Id="1">1</SwitchNo>
<SwitchNo Id="2">1</SwitchNo>
<SwitchNo Id="3">0</SwitchNo>
<SwitchNo Id="4">0</SwitchNo>
<SwitchNo Id="5">0</SwitchNo>
<SwitchNo Id="6">0</SwitchNo>
</Station>
<Station Id="2">
<SwitchNo Id="1">1</SwitchNo>
<SwitchNo Id="2">1</SwitchNo>
<SwitchNo Id="3">1</SwitchNo>
<SwitchNo Id="4">0</SwitchNo>
<SwitchNo Id="5">0</SwitchNo>
<SwitchNo Id="6">0</SwitchNo>
</Station>
<Station Id="3">
<SwitchNo Id="1">1</SwitchNo>
<SwitchNo Id="2">1</SwitchNo>
<SwitchNo Id="3">1</SwitchNo>
<SwitchNo Id="4">0</SwitchNo>
<SwitchNo Id="5">0</SwitchNo>
<SwitchNo Id="6">0</SwitchNo>
</Station>
<Station Id="4">
<SwitchNo Id="1">0</SwitchNo>
<SwitchNo Id="2">0</SwitchNo>
<SwitchNo Id="3">1</SwitchNo>
<SwitchNo Id="4">1</SwitchNo>
<SwitchNo Id="5">0</SwitchNo>
<SwitchNo Id="6">0</SwitchNo>
</Station>
<Station Id="5">
<SwitchNo Id="1">1</SwitchNo>
<SwitchNo Id="2">1</SwitchNo>
<SwitchNo Id="3">1</SwitchNo>
<SwitchNo Id="4">1</SwitchNo>
<SwitchNo Id="5">1</SwitchNo>
<SwitchNo Id="6">0</SwitchNo>
</Station>
<Station Id="6">
<SwitchNo Id="1">1</SwitchNo>
<SwitchNo Id="2">1</SwitchNo>
<SwitchNo Id="3">1</SwitchNo>
<SwitchNo Id="4">1</SwitchNo>
<SwitchNo Id="5">1</SwitchNo>
<SwitchNo Id="6">1</SwitchNo>
</Station>
</Stations>




下面我们就从xml数据的创建/查询/修改这三个方面简单谈一下吧.关于概念啥的就不啰嗦了,直接上代买吧.

1. 创建这个文件



/// <summary>
/// 创建xml文件
/// </summary>
private void writeStationXmlData()
{
string fileName = Path.Combine(dataPath, "StationWitch.xml");

if (!Directory.Exists(dataPath))
Directory.CreateDirectory(dataPath);

if (File.Exists(fileName))
return;

var stationEleList = new List<XElement>();  //临时缓存每个站台节点

//一共6个站台,循环创建
for (int i = 1; i < 7; i++)
{
var xAttr = new XAttribute("Id", i);

//临时缓存每个每个站台下的开关状态
var childList = new List<XElement>();
for (int j = 1; j < 7; j++)
{
childList.Add(new XElement("SwitchNo", new XAttribute("Id", j), i >= j ? 1 : 0));
}

//构建一个站台节点
var stationEle = new XElement("Station", xAttr, childList.ToArray());
stationEleList.Add(stationEle);
}

//构建Stations节点
XElement xBody = new XElement("Stations", stationEleList.ToArray());

//构建整个文档
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"), xBody);
doc.Save(fileName);
}



2. 读取当前xml 文件并转化为友好的类型



/// <summary>
/// 查询读取每个站台的开关状态并转化为keyValue键值对
/// </summary>
/// <returns></returns>
public IList<KeyValue<int, int>> GetStationLightData()
{
writeStationXmlData();
if (stationLightData == null)
{
stationLightData = new List<KeyValue<int, int>>();
stationSwitchStatus = new List<KeyValue<int, bool>>();

//加载xml
string fileName = Path.Combine(dataPath, "StationWitch.xml");
XElement doc = XElement.Load(fileName);

//读取站台列表
var station = from s in doc.Descendants("Station")
select s;
//遍历站台
foreach (var st in station)
{
//获取每个站台的开关集合 即SwitchNo节点
var st_switchs = st.Elements("SwitchNo");
var stationId = int.Parse(st.Attribute("Id").Value);
int light = 0;
foreach (var sw in st_switchs)
{
var swithNo = int.Parse(sw.Attribute("Id").Value);
bool isOn = sw.Value == "1";
if (isOn)
light += 20;
stationSwitchStatus.Add(new KeyValue<int, bool>(stationId, isOn, swithNo));
}
stationLightData.Add(new KeyValue<int, int>(stationId, light));
}
}
return stationLightData;
}



3.更新节点值(先查询找到指定的节点 在更新后保存)



/// <summary>
/// 更新制定站台中的指定开关的状态,即更新制定Station->SwitchNo节点下值
/// </summary>
/// <param name="station"></param>
/// <param name="switchNo"></param>
/// <param name="isOn"></param>
public void UpdateStationSwitchStatus(int station, int switchNo, bool isOn)
{
string fileName = Path.Combine(dataPath, "StationWitch.xml");
//读取数据
XElement doc = XElement.Load(fileName);
var up_station = doc.Descendants("Station")
.Where(o => o.Attribute("Id").Value == station.ToString()).FirstOrDefault();

//找出指定的节点
var upSwitch = up_station.Elements("SwitchNo").FirstOrDefault(o => o.Attribute("Id").Value == switchNo.ToString());

//更新
if (upSwitch != null)
{
upSwitch.Value = isOn ? "1" : "0";
}

//保存为文件
lock (sync)
{
doc.Save(fileName);
}
}



至此,xml的常规操作就介绍完了,希望能对有需要的同学有所帮助.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: