2

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?

2
  • 2
    What do you expect as output?? A list of XmlNode objects? A list of strings?? What?? Commented Feb 8, 2011 at 11:22
  • i want whole Tabs attribute. and then i need to get the value A,B,C. Commented Feb 8, 2011 at 11:25

3 Answers 3

3

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

Check the details on SelectNodes

Sign up to request clarification or add additional context in comments.

3 Comments

thanks. i got it. but can you please tell me how to get Tab Title attribute. for Ex: A to C
@Suresh - Well, this is a perfect candidate for Linq to be used. You need to research a bit on Linq.xml in c# (hope you are using v3.5 or above). using Linq, getting this info is a piece of cake, unfortunately I am not clear on syntax there.
@Suresh - You can still continue to use the selectnodes to get the values as desired by you, but that will be lot of code required. Using Linq its just one line of code for you. :)
1

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;

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.