Groups | Blog | Home
all groups > dotnet xml > july 2005 >

dotnet xml : attribute names



Martin Honnen
7/23/2005 12:00:00 AM


[quoted text, click to view]


[quoted text, click to view]

After you build a DOM there is certainly no need to use a Reader to find
the name of any node in the document, all nodes are in the document tree
and have properties
Name
LocalName
NamespaceURI
so you can simply read out those properties.


--

Martin Honnen --- MVP XML
ian
7/23/2005 5:50:52 AM
Hi, I posted this on the general c# newsgroup a while ago and didn't get
a reply, so dug about and found you all here :)

------Original message----------

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.Item(0).LocalName.ToStrin
g() + " = " + doc.DocumentElement.Attributes.Item(0).Value.ToString());
//*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.




Chris Lovett
7/25/2005 9:04:40 PM
You have to initialize the reader by calling Read() once to move to the
<book> element, then you can move to that element's attributes. Think of
the XmlReader as a scanner.

[quoted text, click to view]

AddThis Social Bookmark Button