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 ;)

Recommended Answers

All 2 Replies

"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

"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. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.