Connecting Tech Pros Worldwide Forums | Help | Site Map

Adding attributes to an element

fix
Guest
 
Posts: n/a
#1: Nov 12 '05
Hi,

I have an XML document and I need to add some attributes to an element.
I use the XMLDocument object to load the xml file, and I can get the
element I need to add attributes to in an XMLNode. The problem is, it
seems that I cannot create a new XmlAttribute, it has no constructor.
Does anyone have any idea how I can do this? I would appreciate your
help. Thanks.

fix.


Oleg Tkachenko [MVP]
Guest
 
Posts: n/a
#2: Nov 12 '05

re: Adding attributes to an element


fix wrote:
[color=blue]
> I have an XML document and I need to add some attributes to an element.
> I use the XMLDocument object to load the xml file, and I can get the
> element I need to add attributes to in an XMLNode. The problem is, it
> seems that I cannot create a new XmlAttribute, it has no constructor.[/color]

What about XmlElement.SetAttribute() method?

--
Oleg Tkachenko [XML MVP, MCAD]
http://www.xmllab.net
http://blog.tkachenko.com
Pascal Schmitt
Guest
 
Posts: n/a
#3: Nov 12 '05

re: Adding attributes to an element


Hello!
[color=blue]
> I have an XML document and I need to add some attributes to an element.
> I use the XMLDocument object to load the xml file, and I can get the
> element I need to add attributes to in an XMLNode. The problem is, it
> seems that I cannot create a new XmlAttribute, it has no constructor.
> Does anyone have any idea how I can do this? I would appreciate your
> help. Thanks.[/color]

You don't use the constructors of XmlNode (and classes that inherit from
it). You have to create the objects using the
XmlDocument.CreateXXX-Methods (like CreateTextNode, CreateElement,...)
and then add them to the Tree.


--
Pascal Schmitt
fix
Guest
 
Posts: n/a
#4: Nov 12 '05

re: Adding attributes to an element


Oh yes, XmlElement object works for me. I was just looking into
XmlNode...... But what's the different between this two? Is XmlElement
only for a "tag" but XmlNode can point to "tags", attributes, etc?

Thanks.
fix.

Oleg Tkachenko [MVP] wrote:
[color=blue]
> fix wrote:
>[color=green]
>> I have an XML document and I need to add some attributes to an element.
>> I use the XMLDocument object to load the xml file, and I can get the
>> element I need to add attributes to in an XMLNode. The problem is, it
>> seems that I cannot create a new XmlAttribute, it has no constructor.[/color]
>
>
> What about XmlElement.SetAttribute() method?
>[/color]

fix
Guest
 
Posts: n/a
#5: Nov 12 '05

re: Adding attributes to an element


Pascal Schmitt wrote:
[color=blue]
> Hello!
>[color=green]
>> I have an XML document and I need to add some attributes to an element.
>> I use the XMLDocument object to load the xml file, and I can get the
>> element I need to add attributes to in an XMLNode. The problem is, it
>> seems that I cannot create a new XmlAttribute, it has no constructor.
>> Does anyone have any idea how I can do this? I would appreciate your
>> help. Thanks.[/color]
>
>
> You don't use the constructors of XmlNode (and classes that inherit from
> it). You have to create the objects using the
> XmlDocument.CreateXXX-Methods (like CreateTextNode, CreateElement,...)
> and then add them to the Tree.
>
>[/color]

I wasn't too sure about the difference between XmlNode and XmlElement. I
am now using XmlElement.SetAttribute and it works. Anyway, thanks.

fix.

Martin Honnen
Guest
 
Posts: n/a
#6: Nov 12 '05

re: Adding attributes to an element




fix wrote:
[color=blue]
> Oh yes, XmlElement object works for me. I was just looking into
> XmlNode...... But what's the different between this two? Is XmlElement
> only for a "tag" but XmlNode can point to "tags", attributes, etc?[/color]

Well, XmlElement is for element nodes, XmlAttribute is for attribute
nodes, XmlText is for text nodes and so on. XmlNode is a common base
class for all those type of nodes. Thus each instance of XmlElement is
also an instance of XmlNode but not the other way round.

The markup of elements consists e.g. of start and end "tag" and the
contents in between
<god>Kibo</god>
but in the object model there is nothing called "tags", there are nodes
of different types where one type is XmlElement for element nodes.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Stefan Misch
Guest
 
Posts: n/a
#7: Nov 12 '05

re: Adding attributes to an element


XmlDocument xmlDoc = new XmlDocument();
XmlNode xmlNode;
// .. some code to create XML doc
xmlNode = xmlDoc.SelectSingleNode("<some XPath expression>");
xmlChild = (XmlElement)xmlNode;
xmlChild.SetAttribute("name", "value");

HTH,
Stefan

"fix" <fix@here.com> schrieb im Newsbeitrag
news:ehkqDkRsFHA.904@tk2msftngp13.phx.gbl...
[color=blue]
> Hi,
>
> I have an XML document and I need to add some attributes to an element.
> I use the XMLDocument object to load the xml file, and I can get the
> element I need to add attributes to in an XMLNode. The problem is, it
> seems that I cannot create a new XmlAttribute, it has no constructor.
> Does anyone have any idea how I can do this? I would appreciate your help.
> Thanks.
>
> fix.
>[/color]


Pascal Schmitt
Guest
 
Posts: n/a
#8: Nov 12 '05

re: Adding attributes to an element


Hello!
[color=blue][color=green]
>> Oh yes, XmlElement object works for me. I was just looking into
>> XmlNode...... But what's the different between this two? Is XmlElement
>> only for a "tag" but XmlNode can point to "tags", attributes, etc?[/color]
>
> Well, XmlElement is for element nodes, XmlAttribute is for attribute
> nodes, XmlText is for text nodes and so on. XmlNode is a common base
> class for all those type of nodes. Thus each instance of XmlElement is
> also an instance of XmlNode but not the other way round.
>
> The markup of elements consists e.g. of start and end "tag" and the
> contents in between
> <god>Kibo</god>
> but in the object model there is nothing called "tags", there are nodes
> of different types where one type is XmlElement for element nodes.[/color]

This is an interesting introduction (and a core reference...):

<http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/introduction.html>


--
Pascal Schmitt

fix
Guest
 
Posts: n/a
#9: Nov 12 '05

re: Adding attributes to an element


Pascal Schmitt wrote:[color=blue]
> Hello!
>[color=green][color=darkred]
>>> Oh yes, XmlElement object works for me. I was just looking into
>>> XmlNode...... But what's the different between this two? Is
>>> XmlElement only for a "tag" but XmlNode can point to "tags",
>>> attributes, etc?[/color]
>>
>>
>> Well, XmlElement is for element nodes, XmlAttribute is for attribute
>> nodes, XmlText is for text nodes and so on. XmlNode is a common base
>> class for all those type of nodes. Thus each instance of XmlElement is
>> also an instance of XmlNode but not the other way round.
>>
>> The markup of elements consists e.g. of start and end "tag" and the
>> contents in between
>> <god>Kibo</god>
>> but in the object model there is nothing called "tags", there are
>> nodes of different types where one type is XmlElement for element nodes.[/color]
>
>
> This is an interesting introduction (and a core reference...):
>
> <http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/introduction.html>
>
>[/color]

All right guys, thanks a lot. Now I have a clearer picture.

fix.

Closed Thread