linq 出错 如何解决

moonshineidolon 2009-09-22 05:04:38
以下代码 没有查询出想要的结果 如何修改

想要查询的内容是 <Layout direction="Vertical"> 这个小结


代码部分

XName Form = "Form", Id = "id", Control = "Control", Layout = "Layout", direction = "direction", Location = "Location", x = "x", y = "y", Size="Size",width = "width", height = "height";
string FormID = "FormLogin", ControlID = "panelMain", Direction = "Vertical";

//IEnumerable<string> partNos = Elements
var LocationList = from Forms in Root.Elements(Form)
where
( (string)Forms.Attribute(Id) == FormID &&
(string)Forms.Element(Control).Attribute(Id) == ControlID &&

(string)Forms.Element(Control).Element(Layout).Attribute(direction) == Direction
)
select new{
X =(int)Forms.Element(Control).Element(Layout).Element(Location).Element(x),
Y =(int)Forms.Element(Control).Element(Layout).Element(Location).Element(y),
Width = (int)Forms.Element(Control).Element(Layout).Element(Size).Element(width),
Height = (int)Forms.Element(Control).Element(Layout).Element(Size).Element(height),

};
xml文件
<?xml version="1.0" encoding="utf-8" ?>
<Root>

<!--登录窗口-->
<Form id="FormLogin" type="Form">

<!--panel控件-->
<Control id ="panelMain" type="Panel">

<Layout direction="Traverse">

<Location>
<x>3</x>
<y>3</y>
</Location>
<Size>
<width>234</width>
<height>262</height>
</Size>

</Layout>



<Layout direction="Vertical">

<Location>
<x>4</x>
<y>34</y>
</Location>
<Size>
<width>234</width>
<height>262</height>
</Size>
</Layout>


</Control>

</Form>

</Root>
...全文
110 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
chen_ya_ping 2009-09-23
  • 打赏
  • 举报
回复

XElement xe = XDocument.Load(Server.MapPath("XMLFile.xml")).Element(XName.Get("Root"));
IEnumerable<XElement> results= xe.Descendants(XName.Get("Layout"));
foreach (XElement result in results)
{
if (result.Attribute("direction").Value == "Vertical")
Response.Write(result.Value);
}
别样苍茫 2009-09-22
  • 打赏
  • 举报
回复
这么乱

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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