Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XmlNode attribute - probably quite an easy one this 1

Status
Not open for further replies.

seantuk

Programmer
Mar 22, 2005
62
0
0
GB
i have this xml

Code:
<html id="sample">
some <i>text</i>
</html>

and i want to read the value of id. i thought this would do it, but it doesn't

Code:
XmlDocument contentFile = new XmlDocument();
			contentFile.Load(Server.MapPath("Index.xml"));

foreach(XmlNode node in contentFile.ChildNodes)
{
  //Console.WriteLine(node.InnerXml);
  Console.WriteLine(node.Attributes["id"]);
}

the statement line is the code i wrote to make sure i had the node the right node. the foreach is for later

how to i get the value of the id attribute?
 
Are you receiving an error or just not getting the proper val?
 
Try,
Console.WriteLine(node.Attributes.Item(0).InnerText);
 
Item(0) is set to null.

i really need a way of referencing it by its name though
 
try

Code:
XmlNodeList nodeList = contentFile.GetElementsByTagName("html");
foreach(XmlNode node in contentFile.ChildNodes)
{
Console.WriteLine(node.Attributes["id"].InnerText); //will give you the value.
}
 
i just found another solution a before lunch (which just finnished a few minutes ago in my country), but frankly if your solution works (it looks good), then i'd rather use that, so probably will next time.

i ended up using XmlTextReader, and basicly coming up with a whole new solution (which i don't actyually like). i assume theres also an innerXML property, or similar, that won't drop my '<i>' tags (etc).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top