shamim
shamim

Reputation: 6768

How to loading XML files from a file

my xml is below:

<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>

my linq syntax is below:

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

From the above xml file i want to get tag attributes values.

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

From the above xml i want to get all tag attributes values .

How to get value from an xml file?

Upvotes: 1

Views: 138

Answers (2)

Ayyash
Ayyash

Reputation: 4399

Or you can in the last step cast to XElement and use whatever XElement has to offer:

instead of 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...
}

if you know one record is going to be return

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...

UPDATE

to make further queries to your result:

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

then loop foreach, you can use menuselement.Element("tag_name").Value to get string values of the nodes, or menuselement.Attribute("attr_name").Value to get attribute values, and you can further query with menuslement.Find or menuselement.Where or menuselement.Select and the options are really limitless... here is where you can learn more: http://msdn.microsoft.com/en-us/library/bb387065.aspx

And here is MSDN's how to query xml using linq: http://msdn.microsoft.com/en-us/library/bb943906.aspx

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500515

Well it looks like you're nearly there:

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

If that doesn't help you, please give more details.

Upvotes: 2

Related Questions