简体   繁体   English

如何在不使用InnerText的情况下将值设置为XmlNode

[英]How to Set value to XmlNode without using InnerText

I want to set value to some XmlNode but i dont want to use InnerText - is there some other way ? 我想设置一些XmlNode的值,但我不想使用InnerText - 还有其他方法吗?

the xml that i need to have is 我需要的xml是

  <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 ? 但我想在不使用InnerText的情况下这样做... =>有没有办法做到这一点?

Thanks 谢谢

Text is instance of one (more more) nodes with node type text. 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: XmlDocument.CreateTextNode包含一个关于如何做到这一点的示例:

//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. 请注意,您可能需要先删除旧的子文本节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM