I'm using VS2005 latest beta, playing around with XML. I wanted a
solution to the question I've seen asked many times which is "How do I
get the attribute's name?" The traditional answer is to use an
XmlNodeReader, although I found a LocalName property on the attribute
when using a DOM.
Here's some code:
XmlDocument doc =new XmlDocument();
XmlNodeReader reader = null;
XmlNode anode = null;
doc.LoadXml("<book genre='novel' ISBN='1-861003-78'
publicationdate='1987'> </book>");
Debug.WriteLine(doc.ChildNodes.Count);
Debug.WriteLine(doc.DocumentElement.Attributes.Ite m(0).LocalName.ToStrin
g() + " = " + doc.DocumentElement.Attributes.Item(0).Value.ToStr ing());
//*1
anode = doc.DocumentElement;
reader = new XmlNodeReader(anode);
reader.MoveToAttribute(0); //*2
Debug.WriteLine(reader.Name);
Debug.WriteLine(reader.ReadContentAsString());
In the above code, the line commented //*1 works and returns the name of
the first attribute and the value. The code at //*2 fails on
MoveToAttribute and if you look in reader, reader.AttributeCount is 0.
I must be missing something fundamental.
Thanks.
*** Sent via Developersdex http://www.developersdex.com ***