How do I get the value from one node in XML using C#

What's the simplest way to get the value of the first node from the attached XML using C#? I expect to get the value ZELNINO. I'm using Visual Studio 2015. I thin it uses C# version 6.

I've tried a bunch of stuff I see on the web but nothing works well.

Also, how would I get the value of the current address street name. The value that should be returned is SPROUL. In this case, there are multiple address elements, but I want the element that has the status element under the address element with a value of CURRENT.
Andrew WerberAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
What you posted is not a file that is in XML format. Please post the correct XML.
Andrew WerberAuthor Commented:
I tried another file. Both display fine in Internet Explorer. I'm also using LINQ for XML and I can use the following code to iterate through the XML as follows:

XDocument doc = XDocument.Load("CreditReportResponse.xml");
foreach (XElement element in doc.Elements().Descendants())
                    string value = element.Value;
Fernando SotoRetiredCommented:
Hi Andrew;

Sorry about that when I opened the file in my iPad the browser removed all the tags. The following code snippet should do what you need.
var xdoc = XDocument.Load(@"C:\....\CreditReportResponse.xml");

// Get the Namespace used in the document which is needed to access the node
XNamespace ns = xdoc.Root.Name.Namespace; 
// The simplest way to get the first node is as follows
var firstNode = xdoc.Descendants(ns + "first").FirstOrDefault();
// If the first node exist write out its value otherwise printout message
string first = firstNode != null ? firstNode.Value : "The first node was not found.";

Open in new window

Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Andrew WerberAuthor Commented:
Thanks for working on this question. My ultimate goal is to be able to the data out of a few nodes.

The question specifically mentions 2 nodes I want to access. My impression is that the best way to get that data is using XPath. I don;t want to loop through all the nodes just to get data from a 'few' nodes.

Your comment just shows how to get the first node. So, that's not quite what I'm looking for.

I will give full credit for the code that accesses the data in both nodes. I'll give half credit for code that gets the data from one of the nodes.
Fernando SotoRetiredCommented:
Here try something like this
var xdoc = XDocument.Load(@"C:\....\CreditReportResponse.xml");

XNamespace ns = xdoc.Root.Name.Namespace;
var results = (from i in xdoc.Root.Descendants(ns + "indicative")
			   let first = i.Descendants(ns + "first").First().Value
			   let streetname = i.Elements(ns + "address").Where(a => a.Element(ns + "status").Value == "current").Descendants(ns + "name").First().Value
			   select new { first, streetname }).FirstOrDefault();

Console.WriteLine("First {0}, Street Name {1}", results.first, results.streetname);

Open in new window

To get other nodes you can follow the same template.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Andrew WerberAuthor Commented:
That worked.

I had the weekend off, and this project is at work, so sorry for the delay.

And, I noticed that my question was poorly written, and I did not specify exactly what node I was looking for with respect to Name, but you got that too.
Fernando SotoRetiredCommented:
Not a problem, glad it worked for you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.