user1658567
user1658567

Reputation: 201

How can i get the inner value of a node in xml

I have an XML like:

<Employee>
<Name> XXXX </Name>
<Address> YYYY </Addeess>
<Country> ZZZZ </Country>
</Employee>

How can i get the inner value of Country? Here Country is dynamically generated(based on user input).

It may be present or not. If present i need to fetch the inner value using c#.

Upvotes: 0

Views: 1217

Answers (2)

Sunny
Sunny

Reputation: 648

This may help you :

XmlDocument ob = new XmlDocument();
ob.Load("yourxmlfile.xml");
XmlNodeList obj_country = ob.GetElementsByTagName("Country");
  if(obj_country!= null)
     {
        string innertext_country_node = obj_country[0].InnerText;
     }

Upvotes: 3

cuongle
cuongle

Reputation: 75316

 var countryElement = XElement.Parse(xml).Element("Country");
 string result = (countryElement != null) ? countryElement.Value : string.Empty;

Upvotes: 1

Related Questions