代码之家  ›  专栏  ›  技术社区  ›  shamim

如何从文件加载XML文件

  •  1
  • shamim  · 技术社区  · 14 年前

    我的xml如下:

    <Demo>
        <ClientCompanyId CompanyId="1">
            <MyMenu>
                <module MenuType="0" ModID="Mod1" ModuleID="1" Perm="False" Text="Basic Settings">
                    <menu MID="1-1" MenuDescription="Mod" MenuType="0" ModuleID="1" ParentID="Mod1" Perm="False" Text="Forms">
                        <Leaf LeafNode="true" MID="1-3" MenuDescription="" MenuType="0" ModuleID="1" ModuleMenuID="1-3" ParentID="1" Perm="False" TargetUrl="" Text="LookUp"/>
                        <submenu MID="1-4" MenuDescription="" MenuType="0" ModuleID="1" ParentID="1" Perm="False" Text="Bank Branch">
                            <Leaf LeafNode="true" MID="1-5" MenuDescription="" MenuType="0" ModuleID="1" ModuleMenuID="1-5" ParentID="4" Perm="False" TargetUrl="" Text="BO Category"/>
                        </submenu>
                    </menu>
                    <menu MID="1-2" MenuDescription="Mod" MenuType="0" ModuleID="1" ParentID="Mod1" Perm="False" Text="Reports">
                        <Leaf LeafNode="true" MID="1-6" MenuDescription="" MenuType="0" ModuleID="1" ModuleMenuID="1-6" ParentID="2" Perm="False" TargetUrl="" Text="Cheque Type"/>
                        <Leaf LeafNode="true" MID="1-7" MenuDescription="" MenuType="0" ModuleID="1" ModuleMenuID="1-7" ParentID="2" Perm="False" TargetUrl="" Text="Stock Exchange"/>
                    </menu>
                </module>
            </MyMenu>
        </ClientCompanyId>
    </Demo>
    

    我的linq语法如下:

     XDocument loaded = XDocument.Load(@"C:\Menu_Settings.xml");
     var q = from c in loaded.Descendants("module")
     where (int)c.Attribute("ModuleID") < 0
     select (string)c.Attribute("Text");
    

    我想从上面的xml文件 标记属性值。

    Text="Basic Settings" ModID="Mod1" ModuleID="1" MenuType="0" Perm="False"
    

    标记属性值。

    文件?

    2 回复  |  直到 8 年前
        1
  •  0
  •   Ayyash    14 年前

    代替var q:

    IEnumerable<XElement> q =from c in loaded.Descendants("module") 
                        where (int)c.Attribute("ModuleID").Value < 0 
                        select c;
    
    foreach(XElement e in q){
       string t = e.Attribute("Text").Value;
       // etc...
    }
    

    如果你知道一张唱片会被退回

    XElement q = (from c in loaded.Descendants("module") 
                        where (int)c.Attribute("ModuleID").Value < 0 
                        select c).First(); // one of many options to return a single record
    
    sring t = q.Attribute("Text").Value;
    // etc...
    

    更新

    要对结果进行进一步查询,请执行以下操作:

    IEnumarble<XElement> menus = q.Elements("menu");
    

    然后循环foreach,您可以使用 menuselement.Element("tag_name").Value 获取节点的字符串值,或 menuselement.Attribute("attr_name").Value 要获取属性值,可以使用menuslement.Find 或 menuselement.Where menuselement.Select http://msdn.microsoft.com/en-us/library/bb387065.aspx

    下面是MSDN如何使用linq查询xml: http://msdn.microsoft.com/en-us/library/bb943906.aspx

        2
  •  2
  •   Jon Skeet    14 年前

    看来你就快到了:

    XDocument loaded = XDocument.Load(@"C:\Menu_Settings.xml");
    
    var q = from c in loaded.Descendants("module")
            where (int)c.Attribute("ModuleID") < 0
            select new
            {
                 Text = (string) c.Attribute("Text"),
                 ModID = (string) c.Attribute("ModID"),
                 ModuleID = (int) c.Attribute("ModuleID"),
                 MenuType = (int) c.Attribute("MenuType"),
                 Perm = (bool) c.Attribute("Perm")
            };