使用 XDocument 加载重复的 XML 属性
我需要使用 XDocument
加载 xml 的帮助。该 xml 保存 WPF 中 HierarchicalDataTemplate
的数据,因此每个元素都具有相同的属性。
我遇到了一个新手问题,如何处理重复的属性 Name、image 和 fileLoc。
我试图让类似下面的代码的东西工作,但正如你所看到的,重复的属性将不起作用。
public static List<MenuItem> Load(string MyMenuFile)
{
var mymenu = XDocument.Load(MyMenuFile).Root.Elements("Menu").Select(
x => new MenuItem(
(string)x.Attribute("id"),
(string)x.Attribute("name"),
(string)x.Attribute("image"),
(string)x.Attribute("fileLoc"),
(string)x.Element("itemlist"),
(string)x.Attribute("name"),
(string)x.Attribute("image"),
(string)x.Attribute("fileLoc"),
(string)x.Element("item"),
(string)x.Attribute("name"),
(string)x.Attribute("image"),
(string)x.Attribute("fileLoc")));
return stationfiles.ToList();
}
这是 xml:
<Menus>
<Menu id="1" Name="Level1" image="C:\lvl1.jpg" fileLoc="C:\lvl1.xml">
</Menu>
<Menu id="2" Name="Level2" image="C:\lvl2.jpg" >
<itemlist Name="Level2" image="C:\lvl2.jpg" fileLoc="C:\lvl2.xml">
</itemlist>
<itemlist Name="Level3" image="C:\lvl3.jpg">
<item Name="First" image="C:\first.jpg" fileLoc="C:\first.xml"></item>
<item Name="Second" image="C:\second.jpg" fileLoc="C:\second.xml"></item>
<item Name="Third" image="C:\third.jpg" fileLoc="C:\third.xml"></item>
</itemlist>
</Menu>
</Menus>
正如您所看到的,元素不同但属性重复。我应该有 3 个单独的类,但是如何将它们组合起来进行 XDocument
加载?任何帮助都会很棒。
I need help loading xml using XDocument
. The xml holds the data for a HierarchicalDataTemplate
in WPF so each element has the same attributes.
I'm having a newbie problem with how to handle the duplicate attributes Name, image and fileLoc.
I was trying to get something like the code below to work, but as you can see duplicate attributes will not work.
public static List<MenuItem> Load(string MyMenuFile)
{
var mymenu = XDocument.Load(MyMenuFile).Root.Elements("Menu").Select(
x => new MenuItem(
(string)x.Attribute("id"),
(string)x.Attribute("name"),
(string)x.Attribute("image"),
(string)x.Attribute("fileLoc"),
(string)x.Element("itemlist"),
(string)x.Attribute("name"),
(string)x.Attribute("image"),
(string)x.Attribute("fileLoc"),
(string)x.Element("item"),
(string)x.Attribute("name"),
(string)x.Attribute("image"),
(string)x.Attribute("fileLoc")));
return stationfiles.ToList();
}
Here is the xml:
<Menus>
<Menu id="1" Name="Level1" image="C:\lvl1.jpg" fileLoc="C:\lvl1.xml">
</Menu>
<Menu id="2" Name="Level2" image="C:\lvl2.jpg" >
<itemlist Name="Level2" image="C:\lvl2.jpg" fileLoc="C:\lvl2.xml">
</itemlist>
<itemlist Name="Level3" image="C:\lvl3.jpg">
<item Name="First" image="C:\first.jpg" fileLoc="C:\first.xml"></item>
<item Name="Second" image="C:\second.jpg" fileLoc="C:\second.xml"></item>
<item Name="Third" image="C:\third.jpg" fileLoc="C:\third.xml"></item>
</itemlist>
</Menu>
</Menus>
As you can see, different elements but duplicate attributes. Should I have 3 separate classes, but how would I combine them for the XDocument
load? Any help would be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这假设这些是直接属于 MenuItem 的元素和属性。我怀疑您需要读取元素 itemslist 和 items 的属性。不知道如何用单个循环来做到这一点。您需要循环遍历元素,然后循环该元素(而不是父元素)的属性。
This assumes those are elements and attributes directly of MenuItem. What I suspect is that you need read attributes of elements itemslist and items. Not sure how to do it with a single loop. You need to loop through the elements and then loop the attribute so THAT element (not the parent element).
您在处理过程中没有等级制度。
我已经调整了您的 xml,但这里是您应该如何处理它的示例:
这是从 linqpad 找到的结果:
查看数据如何解析,这就是您需要如何使用它以将其放入菜单结构中。没有重复的属性。 :-)
HTH
You are not being heirarchical in your processing.
I have adjusted your xml, but here is an example of how you should be processing it:
Here is the result as found from linqpad:
See how the data parses out, that is how you need to work with it to get it into the menu structure. There are no duplicate attributes. :-)
HTH