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

XMLNode --> Adding an Attribute

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");
XmlNode xNode = xmlFile.SelectSingleNode("//a:HeaderShipping", xnsm);

System.Xml.XmlAttribute attribute;
attribute..Name = "accountNumber";
attribute.Value = thirdPartyShipping;

xNode.Attributes.Append(attribute);

Thanks
Andy
Apr 28 '06 #1
3 27296
You didn't say what happeens? Error? No error? If error, what is the
message? Which line is causing the problem?
If no error, what exactly happens vs. what do you expect?

These are all important details!

In any case, your 'attribute' variable is never getting instantiated. So I
would assume the line where you assign the Name property is where you get a
NullReferenceException. That is, if you really only used one dot and not two
when referencing the Name property, otherwise it wouldn't have compiled.

"Andy" <An**@discussions.microsoft.com> wrote in message
news:12**********************************@microsof t.com...
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");
XmlNode xNode = xmlFile.SelectSingleNode("//a:HeaderShipping", xnsm);

System.Xml.XmlAttribute attribute;
attribute..Name = "accountNumber";
attribute.Value = thirdPartyShipping;

xNode.Attributes.Append(attribute);

Thanks
Andy

Apr 28 '06 #2
Andy <An**@discussions.microsoft.com> wrote:
Hello Guys:

What am I doing wrong [...] System.Xml.XmlAttribute attribute;
attribute..Name = "accountNumber";
attribute.Value = thirdPartyShipping;


You need to understand class instances and their creation better.

Declaring a variable of a class type (System.Xml.XmlAttribute)
doesn't create or allocate an object of that type. It only allocates
a reference (which is essentially a pointer that is traced by the
garbage collector) and initializes it with a null reference. You
need to create the object with 'new', as specified by the constructor
which is documented in the class library reference.

HTH/ONI
Apr 28 '06 #3


Andy wrote:

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");
XmlNode xNode = xmlFile.SelectSingleNode("//a:HeaderShipping", xnsm);


The simplest approach is doing e.g.
XmlElement xNode = xmlFile.SelectSingleNode("//a:HeaderShipping",
xnsm) as XmlElement;
xNode.SetAttribute("accountNumber", thirdPartyShipping);

If you really think you need to create the attribute node separately
then use the factory method the XmlDocument instance itself exposes e.g.
XmlAttribute attribute = xmlDocument.CreateAttribute("accountNumber");
attribute.Value = thirdPartyShipping;
XmlElement xNode = xmlFile.SelectSingleNode("//a:HeaderShipping",
xnsm) as XmlElement;
xNode.SetAttributeNode(attribute);

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Apr 29 '06 #4

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

Similar topics

1
by: Carlo | last post by:
I was wondering if someone could help me with this or point me to a good resource. I'm trying to add some attributes to docbook to be used later in effectivity. It was recommended I change the...
2
by: Brad Quinn | last post by:
Lets say I have an XmlNode that validates against some schema. I also have a class that was generated by the xsd tool using said schema. What is the best way to get from an XmlNode to an...
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>...
2
by: Larry Woods | last post by:
I am trying to put a description on a Constructor of a class. All doc says to Import System.ComponentModel, which I do. But in order to get "<Description" to be recognized I have to use...
1
by: Aaron | last post by:
<a> <xsl:attribute name="href"> http://<xsl:value-of select="name" /> </xsl:attribute> <xsl:value-of select="thelink" />
2
by: darrel | last post by:
I am using a javascript WYSIWYG text editor for our CMS. To grab the proper content we need to add a javacsript call to the form: Form1.Attributes.Add("onSubmit", "myOnSubmitEventHandler();") ...
4
by: sam | last post by:
Hi all, When I use the HTML tidy tool in Firefox I see the following warning which I want to get rid of . I cleared all the errors and warnings it showed except for this one. I am not able to...
1
by: =?Utf-8?B?TmFiZWVsIE1vZWVu?= | last post by:
Hi, I want to add the xsi:nil="true' attribute to an element in XML. I am using XmlNode.Attributes.Append() but the generated output results in the attribute ignoring the "xsi:" prefix. How do...
6
by: CSharper | last post by:
I need to set an atribute to an xml element, I know how to do it with XElement.SetAttributeValue method. But What I am trying to do is, I need to add a attribute with a namespace like the following...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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:
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...
0
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,...
0
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...
0
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,...

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.