Linq to xml小问题,在线等,谢谢各位了!菜鸟给你们鞠躬了!

wenjunsu 2012-03-19 08:03:36
我的xml
XML code

<?xml version="1.0" encoding="gb2312"?>
<Configurations version="987654" xmlns="http://www.ytlcn.com">
<SensorDefList>
<SensorDef id="1" name="红外传感器" description="" type="273" address="9a3b2451" />
<SensorDef id="2" name="门廊开关" description="门廊灯" type="258" address="9a3b2452" />
<SensorDef id="3" name="厨房卫生间开关" description="单键开关" type="258" address="9a3b2453" />
<SensorDef id="4" name="客厅开关" description="双键开关" type="260" address="9a3b2454" />
<SensorDef id="5" name="卧室床头开关" description="单键开关" type="258" address="9a3b2455" />
<SensorDef id="6" name="卧室门口开关" description="双键开关" type="260" address="9a3b2456" />
<SensorDef id="7" name="卧室阳台门窗磁" description="" type="272" address="9a3b2457" />
<SensorDef id="8" name="阳台照度传感器" description="" type="514" address="9a3b2458" />
<SensorDef id="9" name="门卡" description="" type="274" address="9a3b2459" />
<SensorDef id="10" name="关门廊灯定时器" description="" type="771" param1="3" />
<SensorDef id="11" name="温感" description="" type="1025" address="9a3b2461" />
<SensorDef id="12" name="天亮时段" description="" type="770" param1="180" param2="e3" />
<SensorDef id="13" name="傍晚时段" description="" type="770" param1="480" param2="e3" />
</SensorDefList>
</Configurations>






我的代码


XDocument doc = new XDocument();
doc = XDocument.Load(AppDomain.CurrentDomain.BaseDirectory + "Configuration.xml");

XNamespace ns = "http://www.ytlcn.com";
List<XElement> elementList = doc.Descendants(ns + "SensorDef").ToList();



然后我有个SensorDef的实体,如下:

public class SensorDef
{
private String Id;

public String id
{
get { return Id; }
set { Id = value; }
}
private String Name;

public String name
{
get { return Name; }
set { Name = value; }
}
private String Description;

public String description
{
get { return Description; }
set { Description = value; }
}
private String Type;

public String type
{
get { return Type; }
set { Type = value; }
}
private String Address;

public String address
{
get { return Address; }
set { Address = value; }
}
private String ParamOne;

public String paramOne
{
get { return ParamOne; }
set { ParamOne = value; }
}
private String ParamTwo;

public String paramTwo
{
get { return ParamTwo; }
set { ParamTwo = value; }
}
}


然后我想把刚才查到的elementList 这个List赋值到List<SensorDef>中,代码如下:


var god = from xl in elementList
select new SensorDef
{
id = xl.Attribute("id").Value,
name = xl.Attribute("name").Value,
description = xl.Attribute("description").Value,
type = xl.Attribute("type").Value,
address = xl.Attribute("address").Value,
paramOne = xl.Attribute("param1").Value,
paramTwo = xl.Attribute("param2").Value,
};
List<SensorDef> sensorList = (List<SensorDef>)god;


但是这么写。linq是错的。请大神帮个忙啊
...全文
197 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
q107770540 2012-03-19
  • 打赏
  • 举报
回复
顶楼上

注意要对节点进行非空判断

http://blog.csdn.net/q107770540/article/details/6117004
孟子E章 2012-03-19
  • 打赏
  • 举报
回复
你可以这样
XNamespace ns = "http://www.ytlcn.com";
IEnumerable<XElement> elementList = doc.Descendants(ns + "SensorDef");

var god = from xl in elementList
select new SensorDef
{
id = xl.Attribute("id").Value,
name = xl.Attribute("name").Value,
description = xl.Attribute("description").Value,
type = xl.Attribute("type").Value,
address = xl.Attribute("address") == null ? "" : xl.Attribute("address").Value,
paramOne = xl.Attribute("param1") == null ? "" : xl.Attribute("param1").Value,
paramTwo = xl.Attribute("param2") == null ? "" : xl.Attribute("param2").Value
};

List<SensorDef> sensorList = god.ToList<SensorDef>();
MessageBox.Show(sensorList.Count.ToString());
孟子E章 2012-03-19
  • 打赏
  • 举报
回复
xl.Attribute("param1")为null了,xl.Attribute("param1").value也就错了
孟子E章 2012-03-19
  • 打赏
  • 举报
回复
出错是因为你的xm中有些节点param1是没有的。
tangxu12 2012-03-19
  • 打赏
  • 举报
回复
把你的问题简化点把!
大家没这么多时间看的

111,093

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧