473,389 Members | 1,089 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,389 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 13524
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: 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: 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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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:
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...

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.