473,322 Members | 1,806 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,322 software developers and data experts.

How to add new attribute to XmlNode

I am reading an XmlFile using XmlDocument and traverse through the
XmlNode, as I read I need to append an attribute to the XmlNode on
some conditions.

I tried xmlNode.Attributes.Append()

It takes only XmlAttribute and in this when I create a new
XmlAttribute, it doesn't allow me to set the name and value as the
name is only the readonly value. How can I achive this?

Jun 27 '08 #1
4 13492
On May 12, 3:55*pm, CSharper <cshar...@gmx.comwrote:
I am reading an XmlFile using XmlDocument and traverse through the
XmlNode, as I read I need to append an attribute to the XmlNode on
some conditions.

I tried xmlNode.Attributes.Append()

It takes only XmlAttribute and in this when I create a new
XmlAttribute, it doesn't allow me to set the name and value as the
name is only the readonly value. How can I achive this?
According to:

http://www.omegacoder.com/?p=102

it can be done.

Google is your friend.
Jun 27 '08 #2
Hi CSharper,

First create the attribute, like xmldoc.CreateAttribute("newattribute"),
update it and, then, add it by CreateChild.

Regards, Alex
[TechBlog] http://devkids.blogspot.com

CI am reading an XmlFile using XmlDocument and traverse through the
CXmlNode, as I read I need to append an attribute to the XmlNode on
Csome conditions.
C>
CI tried xmlNode.Attributes.Append()
C>
CIt takes only XmlAttribute and in this when I create a new
CXmlAttribute, it doesn't allow me to set the name and value as the
Cname is only the readonly value. How can I achive this?
C>
Jun 27 '08 #3
Well, you can only set attributes on elements, so I assume your
XmlNode is actually an XmlElement (XmlElement is a more-specific
XmlNode implementation).

As such, the easiest way to handle attributes is to cast your node to
an element:

XmlNode node = {TODO};
XmlElement el = (XmlElement)node;
el.SetAttribute("Foo", "Bar");

Alternatively (leaving as XmlNode):

XmlNode node = {TODO};
XmlAttribute attrib =
node.OwnerDocument.CreateAttribute("Foo");
attrib.Value = "Bar";
node.Attributes.Append(attrib);

Marc
Jun 27 '08 #4
Heh, had a mistake in the last msg, sorry, no CreateChile for node. Correct
way:
1. Create the attribute: XmlAttribute attr = doc.CreateAttribute("newattribute");
2. Append one: xmlNode.Attributes.Append(attr);

The value can be set after both steps.

Regards, Alex
[TechBlog] http://devkids.blogspot.com

AMHi CSharper,
AM>
AMFirst create the attribute, like
AMxmldoc.CreateAttribute("newattribute"), update it and, then, add it
AMby CreateChild.
AM>
AMRegards, Alex
AM[TechBlog] http://devkids.blogspot.com
C>I am reading an XmlFile using XmlDocument and traverse through the
C>XmlNode, as I read I need to append an attribute to the XmlNode on
C>some conditions.
C>>
C>I tried xmlNode.Attributes.Append()
C>>
C>It takes only XmlAttribute and in this when I create a new
C>XmlAttribute, it doesn't allow me to set the name and value as the
C>name is only the readonly value. How can I achive this?
C>>
Jun 27 '08 #5

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

Similar topics

8
by: Mikey | last post by:
I have an XML document as follows: <Menu> <Group> <Item Text="About Us" AccessRoles="All"> <Group> <Item Text="Option 1" AccessRoles="All" /> <Item Text="Option 2" AccessRoles="All" /> <Item...
4
by: Robert Strickland | last post by:
I have a service that I call that returns back a XmlNode type. I need to get to an attribute value and would like to use the GetAttribute method (because it takes a string). However, XmlNode (or...
1
by: andrej | last post by:
hi, ich habe eine anwendung, welche ein xml document erstellt. um festzustellen, ob ein element bereits vorhanden ist, verwende ich die funktion selectsinglenode( ....) diese funktion...
2
by: Josema | last post by:
Hi, Im trying to get a xmlnode having the value of an attribute, but without success this is my xml file: <promotions> <promotion id="0"> <image>images/gifs/Panel4/Panel4_Prom2.gif</image>...
1
by: Marc | last post by:
Hi! I'm working with a C# client that calls a php web service. I've created a wrapper to call the service using .NET wsdl tool (adding a web reference). The call to the server works fine, it...
3
by: Andy | last post by:
Hello Guys: What am I doing wrong with this code? I can't seem to get it to simply add an attribute to my node. The node already exists. I am simply opening the XMLDocument and creating one...
7
by: Simon Hart | last post by:
Hi, I have a requirement to remove the xmlns from the DOM in order to pass over to MS CRM 3.0 Fetch method.It seems the fetch method blows up if there is a xmlns present!?! The reason I have a...
0
by: =?Utf-8?B?ZGF2aWQ=?= | last post by:
When I create a tree view control from XML document, I use XmlNode.Name in the node list iteration. But sometime, it is supposed that it got the element name. But sometimes it got the element name...
2
by: =?iso-8859-1?Q?Norbert_P=FCrringer?= | last post by:
Hello! Is it possible to use the object XMLNode as a parameter in an interface function of a WCF service? In my case I get the error message: XmlNode ProcessServiceRequest(XmlNode request);...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.