Solved

Problem with setting the value on a node of type element in xml

Posted on 2004-10-14
4
883 Views
Last Modified: 2008-02-01
Hi there,

I am trying to add a new record to my XML file. I was able to create the record and its fields, but when I tried to add values to the fields, I got the error: "Cannot set a value on node type: Element".

I am confident that I have something missing in my code, that it is not making it recognize the ability to set the value of my elements.

Here is a sample of my code:

        XmlDocument SrcDoc = new XmlDocument();
        SrcDoc.Load(xmlfile);
        XmlNode RootNode = SrcDoc.DocumentElement;

        //create a new table node
        XmlNode Newtable = SrcDoc.CreateNode(XmlNodeType.Element, "table", "");
        //create the sub node
        XmlNode ExtNode = SrcDoc.CreateNode(XmlNodeType.Element, "Ext","");
        //fill the data in ----------------------------------my error is here
        ExtNode.Value = newext;   // being passed to the subroutine

        //add the node to the new table node
        Newtable.AppendChild(ExtNode);
        //add the new table node to the root
        RootNode.AppendChild(Newtable);
        //save the docment back to the file
        SrcDoc.Save(xmlfile);


Thank you in advance for your prompt help.

yk
0
Comment
Question by:markataya
  • 2
  • 2
4 Comments
 
LVL 10

Accepted Solution

by:
Hans Langer earned 125 total points
Comment Utility
replace this  

   //create a new table node
        XmlNode Newtable = SrcDoc.CreateNode(XmlNodeType.Element, "table", "");
        //create the sub node
        XmlNode ExtNode = SrcDoc.CreateNode(XmlNodeType.Element, "Ext","");
        //fill the data in ----------------------------------my error is here
        ExtNode.Value = newext;   // being passed to the subroutine

to


     //create a new table node
        XmlNode Newtable = SrcDoc.CreateElement("table");
        //create the sub node
        XmlNode ExtNode = SrcDoc.CreateElement("ext");
        //fill the data in ----------------------------------my error is here
        ExtNode.InnerText= newext;   // being passed to the subroutine


GL
0
 

Author Comment

by:markataya
Comment Utility
Thank you! It works perfectly.

Is it possible to highlight the reason why the second case with CreateElement worked, while the CreateNode did not work?

Thank you again for your prompt response.

-yk
0
 
LVL 10

Expert Comment

by:Hans Langer
Comment Utility
the problem was just de
.value property
must be
.InnerText
Look at this:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemxmlxmlnodeclassvaluetopic.asp
GL
0
 

Author Comment

by:markataya
Comment Utility
Thanks again for all the help!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now