如何从文件加载XML文件

时间:2010-08-11 09:47:23

标签: xml linq linq-to-xml asp.net-3.5

我的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"

从上面的xml我想获得所有 标记属性值。

如何从 xml 文件中获取价值?

2 个答案:

答案 0 :(得分:2)

看起来你好像在那里:

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")
        };

如果这对您没有帮助,请提供更多详细信息。

答案 1 :(得分:0)

或者您可以在最后一步中转换为XElement并使用XElement提供的任何内容:

而不是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.Wheremenuselement.Select并且选项非常无限......这里是您可以了解更多内容的地方: http://msdn.microsoft.com/en-us/library/bb387065.aspx

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