469,286 Members | 2,524 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,286 developers. It's quick & easy.

How to add new node with value?

I want to create new node, and then save it to file.
I can add empty node with attributes, but I don't know how set new value for
the node:
String *strMake = this->txtMake->Text;
String *strModel = this->txtModel->Text;
XmlDocument* XmlDoc = new XmlDocument();

try {
// Get the XML file and load it in the XmlDocument variable
XmlDoc->Load(S"c:\\cars.xml");

// Create the new element
XmlElement *Elm = XmlDoc->CreateElement(S"car");

// Create its attributes
Elm->SetAttribute(S"Make", strMake);
Elm->SetAttribute(S"Model", strModel);

// I try set value like this :
Elm->Value="myvalue";
//but I get message :
// "Cannot set a value on node type : Element."

// Add the new element to the file...
XmlDoc->DocumentElement->AppendChild(Elm);
// ... and save the document
XmlDoc->Save(S"c:\\cars.xml");
}
Nov 12 '05 #1
5 12019
* Lupina wrote in microsoft.public.dotnet.xml:
I want to create new node, and then save it to file.
I can add empty node with attributes, but I don't know how set new value for
the node:


I assume you want to have something like <foo>text</foo>? The "text"
in this example is a text node which generally needs to be created
separately and then appended to the foo element node, see e.g. the
documentation on System.Xml.XmlText.
Nov 12 '05 #2
It works :

.......
XmlText *text = XmlDoc->CreateTextNode("myValue");

XmlDoc->DocumentElement->AppendChild(Elm);

XmlDoc->DocumentElement->LastChild->AppendChild(text);

........

Great Thanks !
Nov 12 '05 #3
See also this example of mine:

http://weblogs.asp.net/sonukapoor/articles/132945.aspx

Sonu Kapoor
Blog: http://weblogs.asp.net/sonukapoor

-----Original Message-----
It works :

.......
XmlText *text = XmlDoc->CreateTextNode("myValue");

XmlDoc->DocumentElement->AppendChild(Elm);

XmlDoc->DocumentElement->LastChild->AppendChild(text);

........

Great Thanks !
.

Nov 12 '05 #4
You can also use XmlElement.InnerText to set its value.

--
Daniel Cazzulino [MVP XML]
Clarius Consulting SA
http://weblogs.asp.net/cazzu
http://aspnet2.com

"Lupina" <lu****@op.pl> wrote in message
news:uD**************@TK2MSFTNGP09.phx.gbl...
It works :

......
XmlText *text = XmlDoc->CreateTextNode("myValue");

XmlDoc->DocumentElement->AppendChild(Elm);

XmlDoc->DocumentElement->LastChild->AppendChild(text);

.......

Great Thanks !

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.690 / Virus Database: 451 - Release Date: 23/05/2004
Nov 12 '05 #5
You can also use XmlElement.InnerText to set its value.

--
Daniel Cazzulino [MVP XML]
Clarius Consulting SA
http://weblogs.asp.net/cazzu
http://aspnet2.com

"Lupina" <lu****@op.pl> wrote in message
news:uD**************@TK2MSFTNGP09.phx.gbl...
It works :

......
XmlText *text = XmlDoc->CreateTextNode("myValue");

XmlDoc->DocumentElement->AppendChild(Elm);

XmlDoc->DocumentElement->LastChild->AppendChild(text);

.......

Great Thanks !

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.690 / Virus Database: 451 - Release Date: 23/05/2004
Nov 12 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by keepyourstupidspam | last post: by
3 posts views Thread by Blaise Garant | last post: by
2 posts views Thread by Keith M | last post: by
reply views Thread by XML newbie: Urgent pls help! | last post: by
reply views Thread by XML newbie: Urgent pls help! | last post: by
1 post views Thread by John Wilhelm | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.