473,386 Members | 2,078 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

attribute names

ian
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.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 ***
Nov 12 '05 #1
2 1037


ian wrote:

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.


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
http://JavaScript.FAQTs.com/
Nov 12 '05 #2
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.

"ian" <no****@devdex.com> wrote in message
news:u1*************@TK2MSFTNGP14.phx.gbl...
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.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 ***

Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
4
by: Gerson Kurz | last post by:
I stumbled across this (while using my homebrewn enum class): class test: pass instance = test() setattr(instance, "THIS :*2+~# IS OBVIOUSLY INVALID", 123) I would've expected some kind of...
2
by: Sean Bright | last post by:
Hi there. I'm having a problem which I hope is a simple one... Any help would be appreciated. I think it has something to do with the way attributes are (or are not) inherited in schemas... ...
0
by: Carl | last post by:
I want to create a generic xslt that would take xml input like: ########################################################################## <?xml version="1.0" encoding="utf-8" ?>...
8
by: Neil Zanella | last post by:
Hello, I would like to know why the W3C decided not to make the following valid XHTML: <input checked type="radio" name="foo" value="bar" />FooBar forcing people to write the following to...
6
by: Alex Hunsley | last post by:
I know that I can catch access to unknown attributes with code something like the following: class example: def __getattr__(self, name): if name == 'age': return __age else: raise...
2
by: gary.goodwin | last post by:
HI I am trying to understand Attribute usage. For example the class SerializableAttribute is a class correct? So why when it is actually u sed the "Attribute" portion of the name is dropped. The...
5
by: emma_middlebrook | last post by:
Hi I've found the rules that cover what is a valid attribute name here: http://www.w3.org/TR/2006/REC-xml-20060816/ 2.3 Common Syntactic Constructs Is there anything already in the dotnet...
3
by: ThunderMusic | last post by:
Hi, I have an XML like document I want to parse... The document looks like this : <Form id="frmMain" width="345" height="75" HeaderHeight="9" image="frmMain.jpg" ColorKey="000000"> <Control...
9
by: Mark Olbert | last post by:
I'm trying to serialize (using XmlSerializer.Serialize) a class that I generated from an XSD schema using XSD.EXE /c. The problem I'm running into is that the root element needs to be unqualified,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.