473,698 Members | 2,631 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding attributes to an element

fix
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.

Nov 12 '05 #1
8 3244
fix wrote:
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.


What about XmlElement.SetA ttribute() method?

--
Oleg Tkachenko [XML MVP, MCAD]
http://www.xmllab.net
http://blog.tkachenko.com
Nov 12 '05 #2
Hello!
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.


You don't use the constructors of XmlNode (and classes that inherit from
it). You have to create the objects using the
XmlDocument.Cre ateXXX-Methods (like CreateTextNode, CreateElement,. ..)
and then add them to the Tree.
--
Pascal Schmitt
Nov 12 '05 #3
fix
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:
fix wrote:
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.

What about XmlElement.SetA ttribute() method?


Nov 12 '05 #4
fix
Pascal Schmitt wrote:
Hello!
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.

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


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

fix.

Nov 12 '05 #5


fix wrote:
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?


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/
Nov 12 '05 #6
XmlDocument xmlDoc = new XmlDocument();
XmlNode xmlNode;
// .. some code to create XML doc
xmlNode = xmlDoc.SelectSi ngleNode("<some XPath expression>");
xmlChild = (XmlElement)xml Node;
xmlChild.SetAtt ribute("name", "value");

HTH,
Stefan

"fix" <fi*@here.com > schrieb im Newsbeitrag
news:eh******** *****@tk2msftng p13.phx.gbl...
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.

Nov 12 '05 #7
Hello!
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?


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.


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

<http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/introduction.ht ml>
--
Pascal Schmitt

Nov 12 '05 #8
fix
Pascal Schmitt wrote:
Hello!
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?

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.

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

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


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

fix.

Nov 12 '05 #9

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

Similar topics

1
2720
by: John L. Clark | last post by:
I am curious as to the rationale, and effect, of having default namespaces not applying (directly) to attributes (see http://www.w3.org/TR/REC-xml-names/#defaulting). Given an attribute without a namespace prefix, what is its namespace, if default namespaces do not apply? Are (either of) prefixed or non-prefixed attributes correct? For example, are the following equivalent: <html:br class="foo"...
2
3398
by: Jordan Willms | last post by:
Hi there. I am working with lom metadata and I am a little confused with how to form the following xml element: <lom xmlns="http://www.imsglobal.org/xsd/imsmd_v1p2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.imsglobal.org/xsd/imsmd_v1p2 imsmd_v1p2p2.xsd"/>
6
2830
by: Amir Hardon | last post by:
I'm new to DOM and can't figure out this thing: I'm trying to add a row to a table with a form field in one of it's cells, but if I'm appending the field to a form it gets out of the table. Can some one tell me what I'm doing wrong? it looks like this: var tbl=document.tbl; var frm=document.frm; var newcell=document.createElement("TD");
10
1740
by: Melissa Mussitsch | last post by:
I've done this before while creating a brand new table. But the code below is not working and I keep getting the error: "Internet Explorer cannot open the Internet site ..... Operation aborted" Here is the code: myBody=document.getElementsByTagName("body").item(0); mytablebody = document.getElementById('product').getElementsByTagName("TBODY" );
2
9303
by: Maersa | last post by:
hi, i'm trying to add "attributes" to some nodes and it works at design time but fails at runtime. XmlDocument doc = new XmlDocument(); XmlElement elem = doc.CreateElement( "root" ); elem.InnerXml = "<first>abcde</first><last>1234</last>";
8
3359
by: Ian Collins | last post by:
Is there a way to do this in IE? In Firefox, one can simply append a node from an XML return to the current document. IE whinges about 'No such interface supported' -- Ian Collins.
2
3113
by: Muzzy | last post by:
Hi, I've used information on these newsgroups to build many pages. So I thought that now that I have my script working (something that I've been working on for about a week), I should post it so that it may help others. If posting this script is against the rules in this group then please accept my appologies. I developped this script so that I can add and remove rows in a table in which I have various input fields and I would use the...
5
4599
by: Water Cooler v2 | last post by:
I know that we can add a single name value entry in app.config or web.config in the configuration/configSettings/appSettings section like so: <add key="key" value="value" /> Question: I want to add a dictionary of name-value pairs. In other words, instead of a single name-value, I want to add a name-value collection in there. I could do this:
11
1805
by: paul.boparai | last post by:
Hi, I have an element that I would like to add to the docbook DTD. This element is a wrapper element that I would like to wrap all of the standard docbook elements. This element is called "changebar". So for example I would like changebar to appear like: <changebar> <chapter> .........
0
8683
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9170
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8904
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6531
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2341
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.