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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing XML attributes

Status
Not open for further replies.

phaseshift

IS-IT--Management
Dec 13, 2004
45
US
I have an XML file like the following:

<idx?
<Node1 attribute="">
<Element1 attribute1="" attribute2="" attribute3="" attribute4=""> </item>
</Node1>

< attribute="">
<Node2 attribute1="" attribute2="" attribute3="" attribute4=""> </item>
</Node2>
</idx>

I need to pasre through and read the attributes of the "item" elements where I have about 15 "item" elements.

I started by using XmlDocument.SelectSingeNode but that only gives me one. I have a nodelist loaded up but can't get the attributes. I know this is a simple thing and when I see it I am going to feel like an idiot. But hey it's monday morning right.

Thanks
 
Your XML example doesn't look well formed, but here is an example I wrote

XmlDocument doc = new XmlDocument();
doc.LoadXml(@"<root>
<category name='name'>
<element att1='dd' att2='Sfw' att3='value' />
</category>
<category name='name2'>
<element att1='value' att2='value' att3='value' />
</category>
</root>");

XmlNode node = doc.SelectSingleNode(@"root/category");


Console.WriteLine(node.SelectSingleNode("element").Attributes["att1"].Value);
 
I got that far but how do you get from element to element?
 
foreach (XmlNode n in doc.SelectNodes(@"//root/category/element"))
{
Console.WriteLine(n.Attributes["att1"].Value);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top