0

I'm using this xml structure:

<park>
  <car title="Ferrari" available="true">
    <url>http://www.ferrari.com/</url>
  </rss>
</park>

And this is my code in C#:

XmlDocument doc = new XmlDocument();
            doc.Load("Settings.xml");
            XmlNodeList list = doc.SelectNodes("/park/car");

            foreach (XmlNode item in list)
            {
                   string x = item["@title"].InnerText;
            }

I just want to get "title" property but i can't get it working. I'm using "@" but without success.

ufo
  • 7
  • 5
  • 1
    there's an error in your xml - `car` isn't closed and `/rss` does not have a starting element. Is that the error you are seeing? – default Dec 09 '13 at 12:23
  • This is outside the scope of your question, but have a look at `XDocument` instead of `XmlDocument`. Have a look at http://stackoverflow.com/questions/1542073/xdocument-or-xmldocument – flindeberg Dec 09 '13 at 13:20

2 Answers2

1

Try this code:

string x = item.Attributes["title"].Value;
Shaharyar
  • 12,254
  • 4
  • 46
  • 66
0

I suggest you to use LINQ to XML for parsing xml:

var xdoc = XDocument.Load("Settings.xml");
var titles = xdoc.XPathSelectElements("//park/car")
                 .Select(c => (string)c.Attribute("title"));

Or without XPath:

var titles = xdoc.Descendants("park")
                 .Elements("car")
                 .Select(c => (string)c.Attribute("title"));
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459