2

I am trying to read a file produced by another developer. The file looks something like this. I am trying to read in the value for 'ProfileName', but when I look at the object in memory, I see null for the Value (capital V) attribute. The only place I can see the string "GolfLeague-Dual" is in the outerxml attribute, but I would have to parse through a bunch of just to get it.

<?xml version="1.0"?>
<TopNode>
  <ProfileSettings>
    <ProfileName value="GolfLeague-Dual" />    
  </ProfileSettings>
</TopNode>

Here is my code to try to read this:

XmlDocument doc = new XmlDocument();
doc.Load(directory + @"\Settings.xml");

XmlElement root = doc.DocumentElement;
XmlNodeList nodes = root.SelectNodes("//ProfileSettings");

foreach (XmlNode node in nodes) {
    Console.WriteLine(node["ProfileName"].Value);
}
1
  • 2
    I highly recommend that you consider switching to the Linq-to-XML dom instead. It's much improved over the old one. Commented Jul 18, 2018 at 17:04

1 Answer 1

4

Your code is trying to get the inner value of the node, not an attribute called value. Try this instead...

foreach (XmlNode node in nodes) {
    Console.WriteLine(node["ProfileName"].Attributes["value"].Value);
}

Here's a working dotnetfiddle...

https://dotnetfiddle.net/pmJKbX

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

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.