all groups > dotnet xml > march 2005 >
You're in the

dotnet xml

group:

Getting Attribute value by using name


Getting Attribute value by using name Sam
3/4/2005 8:05:03 AM
dotnet xml: I am getting the element node list, and if any attributes are there, I am
getting the attributes values by using the index
eg
0- first element
1- second element
aNode.Attributes.Item(1).InnerText

But for some reason the order of the attributes is swapped , so my question
is , how to get the attribute value using a name instead of the index values.

I am new to xml and thanks for any help.

eg .
addressNodeList =
xmlDoc.DocumentElement.GetElementsByTagName("UEP_STG_ADDRESS");
Console.WriteLine("Count=" + addressNodeList.Count);
foreach (XmlNode aNode in addressNodeList) {
if (aNode.Attributes.Count > 0 ) {
//1ADDR_TYPE_1
if (aNode.Attributes.Item(1).InnerText == ""){
oRowSBAD["ADDR_TYPE_1"] = DBNull.Value;
}
else{
oRowSBAD["ADDR_TYPE_1"] = aNode.Attributes.Item(1).InnerText.ToString();
}
}
}

Re: Getting Attribute value by using name Sam
3/4/2005 11:23:06 AM
Thank you Martin,

This solved the problem of swapping.

If the attribute is not passed at all (there is no attribute with that
name), it still errors out. Is there a way to solve this.

Sam :)


[quoted text, click to view]
Re: Getting Attribute value by using name Sam
3/4/2005 12:11:01 PM
got it, thank you.

[quoted text, click to view]
Re: Getting Attribute value by using name Martin Honnen
3/4/2005 5:48:57 PM


[quoted text, click to view]

It is simply
elementNode.Attributes["attributeName"].Value
in C#. If the attribute is in a namespace then you need
elementNode.Attributes["localname", "namespaceURI"].Value

--

Martin Honnen
Re: Getting Attribute value by using name Martin Honnen
3/5/2005 12:34:03 PM


[quoted text, click to view]


[quoted text, click to view]

Compare to null then e.g.
XmlAttribute attribute = xmlElement.Attributes["attributename"];
if (attribute != null) {
...
}
--

Martin Honnen
AddThis Social Bookmark Button