使用 XDocument 加载重复的 XML 属性

发布于 2024-12-22 16:01:26 字数 1812 浏览 1 评论 0原文

我需要使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉城已无爱 2024-12-29 16:01:26

这假设这些是直接属于 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).

清音悠歌 2024-12-29 16:01:26

您在处理过程中没有等级制度。

我已经调整了您的 xml,但这里是您应该如何处理它的示例:

string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<Menus> 
    <Menu id=""1""  Name=""Level1 - Alpha"" image=""C:\lvl1.jpg"" fileLoc=""C:\lvl1.xml""/> 
    <Menu id=""2""  Name=""Level1 - Beta"" image=""C:\lvl2.jpg"" fileLoc=""C:\lvl1.xml"" > 
        <itemlist Name=""Level2-Gamma"" image=""C:\lvl2.jpg"" fileLoc=""C:\lvl2.xml""/>  
        <itemlist Name=""Level3-Zeta"" image=""C:\lvl3.jpg"" fileLoc=""C:\lvl1.xml""> 
            <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>";

var xd = XDocument.Parse(xml);

var result = 

xd.Descendants("Menu")
  .Select (l1 => new 
  {
   Name     = l1.Attribute("Name").Value, 
   Image    = l1.Attribute("image").Value, 
   File     = l1.Attribute("fileLoc"),
   Children = l1.Descendants("itemlist")
                  .Select (l2 => new {
                                Name     = l2.Attribute("Name").Value, 
                                Image    = l2.Attribute("image").Value, 
                                File     = l2.Attribute("fileLoc"),
                                Children = l2.Descendants("item")
                                                .Select (l3 => new {
                                                        Name  = l3.Attribute("Name").Value, 
                                                        Image = l3.Attribute("image").Value, 
                                                        File  = l3.Attribute("fileLoc")
                                                                    })
                  })

});

Console.WriteLine (result );

这是从 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:

string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<Menus> 
    <Menu id=""1""  Name=""Level1 - Alpha"" image=""C:\lvl1.jpg"" fileLoc=""C:\lvl1.xml""/> 
    <Menu id=""2""  Name=""Level1 - Beta"" image=""C:\lvl2.jpg"" fileLoc=""C:\lvl1.xml"" > 
        <itemlist Name=""Level2-Gamma"" image=""C:\lvl2.jpg"" fileLoc=""C:\lvl2.xml""/>  
        <itemlist Name=""Level3-Zeta"" image=""C:\lvl3.jpg"" fileLoc=""C:\lvl1.xml""> 
            <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>";

var xd = XDocument.Parse(xml);

var result = 

xd.Descendants("Menu")
  .Select (l1 => new 
  {
   Name     = l1.Attribute("Name").Value, 
   Image    = l1.Attribute("image").Value, 
   File     = l1.Attribute("fileLoc"),
   Children = l1.Descendants("itemlist")
                  .Select (l2 => new {
                                Name     = l2.Attribute("Name").Value, 
                                Image    = l2.Attribute("image").Value, 
                                File     = l2.Attribute("fileLoc"),
                                Children = l2.Descendants("item")
                                                .Select (l3 => new {
                                                        Name  = l3.Attribute("Name").Value, 
                                                        Image = l3.Attribute("image").Value, 
                                                        File  = l3.Attribute("fileLoc")
                                                                    })
                  })

});

Console.WriteLine (result );

Here is the result as found from linqpad:

enter image description here

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文