Suresh Chaudhary
Suresh Chaudhary

Reputation: 1639

get node from xml in c#

I have an xml file like:

<?xml version="1.0" encoding="utf-8"?>
<Config>   
   <MetadataFormConfig FieldInternalName="Test">
      <Tabs>
         <Tab Title="A to C" Order="1">
            <ShowParentTerm>A</ShowParentTerm>
            <ShowParentTerm>B</ShowParentTerm>
            <ShowParentTerm>C</ShowParentTerm>
         </Tab>
         <Tab Title="D to E" Order="2">
            <ShowParentTerm>D</ShowParentTerm>
            <ShowParentTerm>E</ShowParentTerm>
         </Tab>
      </Tabs>   
   </MetadataFormConfig>  
</Config>

I want to get all the nodes by FieldInternalName.

Can please give a way how I can do this?

Upvotes: 2

Views: 572

Answers (3)

santosh singh
santosh singh

Reputation: 28642

Linq version for getting all the nodes by FieldInternalName.

 // Loading from a file, you can also load from a stream
        XDocument loaded = XDocument.Load(@"d:\test.xml");
        // Query the data 
        var query = from c in loaded.Descendants("MetadataFormConfig")
                where (string)c.Attribute("FieldInternalName") == "Test"
                select c;

Upvotes: 1

Davide Piras
Davide Piras

Reputation: 44595

you can test your own xpath expression and refine it until you get your desired result, there are plenty of XPATH testers online, for example here is one: http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm

just go there, paste your xml fragment from above and work with XPATH untill you get what you need.

Upvotes: 0

Sachin Shanbhag
Sachin Shanbhag

Reputation: 55479

You can use SelectNodes("/Config/MetadataFormConfig[@FieldInternalName='Test']")

Check the details on SelectNodes

Upvotes: 3

Related Questions