Solved

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

Posted on 2004-10-14
4
899 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
ID: 12312422
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
ID: 12319669
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
ID: 12319794
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
ID: 12321118
Thanks again for all the help!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

735 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