473,796 Members | 2,765 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Attribu tes.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 13632
On May 12, 3:55*pm, CSharper <cshar...@gmx.c omwrote:
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.Attribu tes.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.CreateAt tribute("newatt ribute"),
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.Attribu tes.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)nod e;
el.SetAttribute ("Foo", "Bar");

Alternatively (leaving as XmlNode):

XmlNode node = {TODO};
XmlAttribute attrib =
node.OwnerDocum ent.CreateAttri bute("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.CreateAttri bute("newattrib ute");
2. Append one: xmlNode.Attribu tes.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.Create Attribute("newa ttribute"), 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.Attribu tes.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
2643
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 Text="Option 3" AccessRoles="All" /> </Group>
4
3944
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 XmlElement) does not have that method. They have the attributes collection with the item method that takes an number value performing an ordinal lookup. I can declare an Xml document, load the returning Xml stream, and then use the method but I do...
1
4744
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 liefert mir ein element des typs node.
2
13979
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> <text>Text of promotión 1</text>
1
18210
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 is serialized correctly, and the server returns a response (I've captured the response and it's correct!) but when the .NET deserialize this response, it throws the exception "System.InvalidOperationException: There is an error in XML
3
27327
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 attribute to the document. I am not creating the document new XmlNamespaceManager xnsm = new XmlNamespaceManager(xmlFile.NameTable); xnsm.AddNamespace("a", "http://www.icsm.com/icsmxml");
7
2981
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 xmlns present is because the Xml I am passing to CRM is a node from a bigger file that does require a xmlns and using the DOM ..OuterXml seems to set the xmlns for you automatically - which I don't want. Any help would be great.
0
1202
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 and first attribute together. For eaxmple, I use the following iteration code after creating the root from DOM.DocumentElement.Name. ----- private void AddNode2(XmlNode inXmlNode, TreeNode inTreeNode) { XmlNode xNode;
2
4629
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); Unable to serialize type "System.Xml.XmlNode". Use the attribute
0
9524
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10449
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
10217
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
10168
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,...
0
9047
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6785
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
5440
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...
0
5568
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2924
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.