日期:2014-05-20  浏览次数:20790 次

linq to xml
C# code
<?xml version='1.0' encoding='UTF-8'?>
<result status="ok">
<order id="5141" orderNo="jpa110506120442266" allPrice="1379" status="CANCEL_OK" contact="刘畅如苑" contactMob="15910399872" contactEmail="" payStatus="0" createTime="2011-05-06 12:04:42.0" needPS="true"><flight code="CA1860" cabin="W" dep="LZH" arr="PEK" depDay="2011-05-09" depTime="15:35" arrTime="18:45" pnr="JEZ8EZ" cpnr="" realCode="" viewPrice="1180" price="1179.0" constructionFee="50" fuelTax="110" childFuelTax="60" policyType="SP"/><passenger name="刘畅如苑" ageType="0" cardType="NI" cardNum="450204198807020624" eticketNum="" insuranceCount="1" insuranceNo=""/><log operator="System" time="2011-05-06 13:07:32.0" action="将超时订单自动删除"/><log operator="彭松" time="2011-05-06 12:06:29.0" action="修改订单,将【订单状态】由【等待座位确认】修改为【订座成功等待支付】;"/><log operator="System" time="2011-05-06 12:05:11.0" action="PATA校验异常,原因:无价格,PATA为没有符合条件的运价, 请使用手工处理  
b。将订单状态改为【等待座位确认】"/><log operator="System" time="2011-05-06 12:04:44.0" action="创建订单!"/></order></result>

这是我的xml文件
C# code
XElement xml = XElement.Load("../../tts.xml");
            var result = from t in xml.Descendants("result")
                         select new { 
                                status=t.Attribute("status").Value                        
                         
                         };
            if (result!=null && result.Count()>0 && result.First().status=="ok")
            {...}

我想判断这个xml文件的result节点的status值 为啥 我这样写老是result==null、

------解决方案--------------------
文档的话,前面有 <?xml version='1.0' encoding='UTF-8'?>
元素的话,加上这个头就是非法的。
------解决方案--------------------
通过 XElement.Load 将 XML 文档加载到 XElement 中时,位于 XML 树根部的 XElement 包含所加载文档的根元素。 然而,通过 XDocument.Load 将同一个 XML 文档加载到 XDocument 中时,树根部为 XDocument 节点,所加载文档的根元素为 XDocument 所允许的一个子 XElement 节点。 LINQ to XML 轴相对于根节点进行操作。
http://msdn.microsoft.com/zh-cn/library/bb675196(v=vs.90).aspx

------解决方案--------------------
向督察学习!