0

XmlNode に値を設定したいのですが、InnerText を使用したくありません。他の方法はありますか?

私が持つ必要があるxmlは

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

だから私はこれをやった

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

しかし、InnerTextを使わずにやりたい... =>それを行う方法はありますか?

ありがとう

4

1 に答える 1

2

テキストは、ノード タイプがテキストの 1 つ (複数) のノードのインスタンスです。したがって、必要に応じて、テキスト ノードを要素に直接追加/置換できます。

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

最初に古い子テキスト ノードを削除する必要がある場合があることに注意してください。

于 2013-10-21T05:23:26.010 に答える