retrieving an attribute from a node in a nodelist

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I can't seem to get the syntax correct. I have an xmlnodelist full of
elements called "file". Each file has an attribute called "ID". If I just
want to get, say, the ID of the second file in the list, how do I write this?

Thanks!!!
Mel
 
Mel,

You should be able to use an XPath query, like so:

file[@ID]

You can use the SelectSingleNode method (or other select node methods)
to select the attribute(s).

Hope this helps.
 
here is an example from my code
private void LoadExtensions( string url, bool merging,bool isString )

{

ArrayList extens = new ArrayList();

XmlTextReader reader;

if(!isString)

{

Stream stream = MakeStream(url); // this bascially opens the file that has
xml

reader = new XmlTextReader(stream);//this reads the stream (file)

}

else

{

reader = new XmlTextReader(url); //if its url then i don t need to open the
file etc

}

XmlDocument doc = new XmlDocument();

doc.Load(reader);

XmlNodeList nodeLst = doc.GetElementsByTagName("WAMiConf"); //top node


if( nodeLst.Count != 1 )

throw new Exception("Invalid XML data. A single WAMiConf tag is required.");

mErrors = new ArrayList();

foreach( XmlNode topNode in nodeLst )

{

foreach( XmlNode extNode in topNode.ChildNodes )

{

if( extNode.Name == "Extensions" )

extens.AddRange( ParseExtensions( extNode ) );

}

}


}


private ArrayList ParseExtensions( XmlNode extenNode )

{

ArrayList extens = new ArrayList();

foreach( XmlNode node in extenNode.ChildNodes )

{

if( node.Name.ToLower() == "extension" )

{

InternalExtension extension = new InternalExtension();

// Look for optional "group" and "maxoccurs" attributes

if( node.Attributes["group"] != null )

extension.GroupName = node.Attributes["group"].Value;

foreach( XmlNode child in node.ChildNodes )

{

if( child.Name.ToLower() == "name" )

extension.Name = child.InnerText;

if( child.Name.ToLower() == "number" )

extension.ExtensionNumber = child.InnerText;

}


}

return extens;

}



forgive me if there are syntax or mismatching braces i just copied and
cleaned up the code for simplicity, otherwise it would be two pages long and
hard to understand.

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top