日期:2014-05-18  浏览次数:20866 次

Linq to xml小问题,在线等,谢谢各位了!菜鸟给你们鞠躬了!
我的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>




我的代码

C# code

            XDocument doc = new XDocument();
            doc = XDocument.Load(AppDomain.CurrentDomain.BaseDirectory + "Configuration.xml");
            List<XElement> elementList = doc.Descendants("SensorDef").ToList();



为什么elementList个数为O,没有找到??给个例子。谢谢。在线等



------解决方案--------------------
你没有加名称空间。你应该这样

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