0

Hi,

I have some data stored on an XML file and I am trying to read a specific part of it.

Here is a line from the XML:-

<photo id="4539802593" owner="***" secret="***" server="4032" farm="5" title="Stone Bridge" ispublic="1" isfriend="0" isfamily="0" />

Below is the bare bones of code I am using to read the file:-

void Button2Click(object sender, EventArgs e)
		{
			XmlDocument xDoc = new XmlDocument();
            xDoc.Load(System.IO.Path.Combine(Application.StartupPath, "api.xml"));

            XmlNodeList photo = xDoc.GetElementsByTagName("photo");
            
            MessageBox.Show(photo[0].OuterXml);
		}

It loads and reads the file without a problem but obviously reads the whole line.

I would like it to just read the {id=} bit.

I have tried putting

XmlNodeList id = xDoc.GetElementsByTagName("id");
            
                           MessageBox.Show(id[0].OuterXml);

but just get an error.

Kind regards..,

MT ;)

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by Cap'nKirk
1

"Id" is an attribute not a tag. That's the mistake you are making.

This code will work for you:

MessageBox.Show(photo[0].Attributes["id"].Value);

Thanks

0

"Id" is an attribute not a tag. That's the mistake you are making.

This code will work for you:

MessageBox.Show(photo[0].Attributes["id"].Value);

Thanks

Once again, thank you Farooq. :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.