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

C# 如何在不使用InnerText的情况下将值设置为XmlNode,c#,xml,C#,Xml,我想将值设置为某个XmlNode,但我不想使用InnerText—还有其他方法吗 我需要的xml是 <ns1:id>123456</ns1:id> 但是我想不使用InnerText来完成它…=>有办法吗 谢谢Text是一个(多个)节点类型为Text的节点的实例。因此,如果需要,可以直接将文本节点追加/替换到元素中 包含一个关于如何做到这一点的示例: //Create a new node and add it to the document. //The text

我想将值设置为某个XmlNode,但我不想使用InnerText—还有其他方法吗

我需要的xml是

  <ns1:id>123456</ns1:id>
但是我想不使用InnerText来完成它…=>有办法吗

谢谢

Text是一个(多个)节点类型为Text的节点的实例。因此,如果需要,可以直接将文本节点追加/替换到元素中

包含一个关于如何做到这一点的示例:

//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);

请注意,您可能需要先删除旧的子文本节点。

也许只需使用
属性,然后尝试使用值。。。不工作:(
Value
可能无法与XmlElement一起正常工作(至少我现在记得是这样)。另一种选择是
InnerXml
。我觉得OP想要的不仅仅是另一个名称不同的属性。如果不是,我想知道如果它完成了任务,为什么
InnerText
是不可取的。我想你应该能够…同时提供
InnerText
不可接受的原因可能会有助于回答。尝试
节点.FirstChild.Value
//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);