0

I want to set value to some XmlNode but i dont want to use InnerText - is there some other way ?

the xml that i need to have is

  <ns1:id>123456</ns1:id>

so i did this

   XmlNode node = doc.CreateElement( doc.DocumentElement.Prefix, "id", doc.DocumentElement.NamespaceURI );
   node.InnerText = "123456";

but i want to do it without using the InnerText ... => is there a way to do it ?

Thanks

Yanshof
  • 9,659
  • 21
  • 95
  • 195
  • Perhaps just use the `Value` property http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.value.aspx – Chris Moutray Oct 21 '13 at 05:16
  • try to use the value ... not working :( – Yanshof Oct 21 '13 at 05:17
  • `Value` might not work properly with XmlElement (at least that's what I recall at the moment). An alternative would be `InnerXml`. I feel OP wants something else than just another property with a different name. And if not, I wonder why `InnerText` is not desirable if it gets the job done. – S_F Oct 21 '13 at 05:18
  • I think you should be able to [append text node](http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.createtextnode.aspx)... Also providing reason why `InnerText` is not acceptable may help with answers. – Alexei Levenkov Oct 21 '13 at 05:19
  • http://stackoverflow.com/q/7877609/81053 try `node.FirstChild.Value` – Chris Moutray Oct 21 '13 at 05:20

1 Answers1

2

Text is instance of one (more more) nodes with node type text. So if you want you can directly append/replace text nodes to your element.

XmlDocument.CreateTextNode contains a sample on how one can do that:

//Create a new node and add it to the document. 
//The text node is the content of the price element.
XmlElement elem = doc.CreateElement("price");
XmlText text = doc.CreateTextNode("19.95");
doc.DocumentElement.AppendChild(elem);
doc.DocumentElement.LastChild.AppendChild(text);

Note that you may need to remove old child text nodes first.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179