2013-03-22 47 views
1

我只是有一個關於將設置寫入我的XML文件中的節點的一個簡單問題,出於某種原因,我的所有其他設置保存,但這一個和我試圖保存的值是(ListingRid = 1 +),(PictureCount = 1 +)在我的代碼textBoxQuery.Text包含(ListingRid = 1 +),(PictureCount = 1 +)爲什麼只有最後一個節點無法寫入XmlDocument?

示例XML

<setting name="SearchQuery" serializeAs="String"> 
<value></value> 
</setting> 

這裏是我打電話的代碼誰能當值可能會告訴我有可能是無效的字符,這就是爲什麼設置沒有保存?

XmlDocument doc = new XmlDocument(); 
doc.Load(path); 
foreach (XmlNode node in doc.SelectNodes("//setting")) 
{ 
    if (node.OuterXml.Contains("SearchQuery")) 
{ 
    node.LastChild.InnerText = textBoxQuery.Text; 
} 
doc.Save(path); //I have this in there at the end. 

回答

0

我改變了代碼來調用由值實際節點屬性,而不是使用,這似乎已經解決了這一問題OuterXml.Contains的。

  if (node.Attributes["name"].Value == ("SearchQuery")) 
      node.LastChild.Innertext = textBoxQuery.Text; 
0

有用於textBoxQuery.Text是無效在這種情況下

node.LastChild.InnerText = textBoxQuery.Text

因爲當它們設置的innerText轉義值沒有值。這是InnerXmlInnerText之間的差異

0

你忘了這個嗎?

doc.Save(path); 
+1

HMM的OP說,*我所有的設置保存,但這個*,這樣應該不會吧... – 2013-03-22 22:12:17

+0

除非他呼籲保存爲其他設置,但沒有這一項。但我看到他編輯了他的帖子並添加了它。 – 2013-03-23 04:19:31

相關問題