2013-03-04 50 views
0

使用SetElementValue這是我的XML如何在的XDocument

<?xml version="1.0"?> 
<SearchHistory> 
    <Search> 
     <title>Text 1</title> 
     <count>0</count> 
    </Search> 
    <Search> 
     <title>Text 2</title> 
     <count>0</count> 
    </Search> 
</SearchHistory> 

的樣品,我想改變的計數文本值1

這是我的嘗試:

xdoc.Descendants("Search") 
    .Where(x => x.Element("title").Value == tbSearch.Text) 
    .Single().SetElementValue("count", curCount); 
+0

問題是什麼?除非'''元素'值與'tbSearch.Text'匹配,否則這應該按照您的預期行事。 – <span class="text-secondary"> <small> <span>2013-03-04 14:14:28</span> </small> </span> </p> </div> </div> </div> <div itemprop="comment" class="post-comment"> <div class="row"> <div class="col-lg-1"><span class="text-secondary">+0</span></div> <div class="col-lg-11"> <p class="commenttext">@FrédéricHamidi他們匹配...如果我把錯誤的價值tbSearch.Text它扔我異常...我沒有得到任何錯誤,它只是不會改變計數值 – <span class="text-secondary"> <small> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/1424127/">a1204773</a></span> <span>2013-03-04 14:16:17</span> </small> </span> </p> </div> </div> </div> <div itemprop="comment" class="post-comment"> <div class="row"> <div class="col-lg-1"><span class="text-secondary">+0</span></div> <div class="col-lg-11"> <p class="commenttext">@Loclip,你如何驗證計數值已經改變或沒有? – <span class="text-secondary"> <small> <span>2013-03-04 14:18:10</span> </small> </span> </p> </div> </div> </div> </div> </div> </article> </div> <div class="answer-title"> <span class="text-logo margin-top-sm">A</span> <h2 class="title h4">回答</h2> </div> <div class="item-description text-md markdown-body margin-bottom-40 voidso"> <article class="board-top-1 padding-top-10"> <div class="post-col vote-info"> <span class="count">3<i class="fa fa-thumbs-up"></i></span> <i class="fa fa-check fa-2x"></i> </div> <div class="post-offset"> <div class="answer fmt"> <p>您必須在修改後保存<code class="prettyprint-override">XDocument</code></p> <pre><code class="prettyprint-override">xDoc.Save(fileName); </code></pre> </div> <div class="post-info"> <div class="post-meta row"> <p class="text-secondary col-lg-6"> <span class="source"> <a rel="noopener" target="_blank" href="https://stackoverflow.com/q/15203616">來源</a> </span> </p> <p class="text-secondary col-lg-6"> <span class="float-right date"> <span>2013-03-04 14:17:22</span> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/424226/">Mark</a></span> </p> <p class="col-12"></p> <p class="col-12"></p></div> </div> <!-- comments --> <div class="comments"> <div itemprop="comment" class="post-comment"> <div class="row"> <div class="col-lg-1"><span class="text-secondary">+1</span></div> <div class="col-lg-11"> <p class="commenttext">爲什麼選擇DownVote?我已經嘗試過他的代碼,並正確調用保存價值是在文件中更改.... – <span class="text-secondary"> <small> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/424226/">Mark</a></span> <span>2013-03-04 14:20:28</span> </small> </span> </p> </div> </div> </div> <div itemprop="comment" class="post-comment"> <div class="row"> <div class="col-lg-1"><span class="text-secondary">+1</span></div> <div class="col-lg-11"> <p class="commenttext">我不知道他們爲什麼downvoted ...這實際上是唯一的答案工作:D – <span class="text-secondary"> <small> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/1424127/">a1204773</a></span> <span>2013-03-04 14:29:50</span> </small> </span> </p> </div> </div> </div> <div itemprop="comment" class="post-comment"> <div class="row"> <div class="col-lg-1"><span class="text-secondary">+0</span></div> <div class="col-lg-11"> <p class="commenttext">所以我的代碼,並通過添加這個代碼在最後很好幫助 – <span class="text-secondary"> <small> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/1424127/">a1204773</a></span> <span>2013-03-04 14:32:33</span> </small> </span> </p> </div> </div> </div> </div> </div> </article> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-6208739752673518" data-ad-slot="1038284119" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <article class="board-top-1 padding-top-10"> <div class="post-col vote-info"> <span class="count">0<i class="fa fa-thumbs-up"></i></span> </div> <div class="post-offset"> <div class="answer fmt"> <pre><code class="prettyprint-override">xdoc.Descendants("Search") .Where(x => x.Element("title").Value == tbSearch.Text) .Single() .Descendants("count") .Single() .Value = "1"; </code></pre> </div> <div class="post-info"> <div class="post-meta row"> <p class="text-secondary col-lg-6"> <span class="source"> <a rel="noopener" target="_blank" href="https://stackoverflow.com/q/15203734">來源</a> </span> </p> <p class="text-secondary col-lg-6"> <span class="float-right date"> <span>2013-03-04 14:22:49</span> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/848634/">polkduran</a></span> </p> <p class="col-12"></p> <p class="col-12"></p></div> </div> <!-- comments --> <div class="comments"> <div itemprop="comment" class="post-comment"> <div class="row"> <div class="col-lg-1"><span class="text-secondary">+0</span></div> <div class="col-lg-11"> <p class="commenttext">其不工作..不改變計數值 – <span class="text-secondary"> <small> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/1424127/">a1204773</a></span> <span>2013-03-04 14:27:31</span> </small> </span> </p> </div> </div> </div> <div itemprop="comment" class="post-comment"> <div class="row"> <div class="col-lg-1"><span class="text-secondary">+0</span></div> <div class="col-lg-11"> <p class="commenttext">你如何測試? – <span class="text-secondary"> <small> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/848634/">polkduran</a></span> <span>2013-03-04 14:35:41</span> </small> </span> </p> </div> </div> </div> <div itemprop="comment" class="post-comment"> <div class="row"> <div class="col-lg-1"><span class="text-secondary">+0</span></div> <div class="col-lg-11"> <p class="commenttext">這隻會改變運行時的值,如果你正在從一個XML文件開始工作,你需要重寫你的'XDocument' – <span class="text-secondary"> <small> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/848634/">polkduran</a></span> <span>2013-03-04 14:37:03</span> </small> </span> </p> </div> </div> </div> </div> </div> </article> </div> <div class="clearfix"> </div> <div class="relative-box"> <div class="relative">相關問題</div> <ul class="relative_list"> <li> 1. <a href="http://hk.uwenku.com/question/p-zggfnylc-ng.html" target="_blank" title="如何過濾Xdocument並返回Xdocument?"> 如何過濾Xdocument並返回Xdocument? </a> </li> <li> 2. <a href="http://hk.uwenku.com/question/p-oyqicuty-sm.html" target="_blank" title="如何枚舉XDocument?"> 如何枚舉XDocument? </a> </li> <li> 3. <a href="http://hk.uwenku.com/question/p-olwoksgf-tp.html" target="_blank" title="如何使用的XDocument"> 如何使用的XDocument </a> </li> <li> 4. <a href="http://hk.uwenku.com/question/p-picxcvrg-bmv.html" target="_blank" title="如何改變用的XDocument"> 如何改變用的XDocument </a> </li> <li> 5. <a href="http://hk.uwenku.com/question/p-anljdhdr-bhc.html" target="_blank" title="xdocument添加xdocument"> xdocument添加xdocument </a> </li> <li> 6. <a href="http://hk.uwenku.com/question/p-uhpmgaeg-rz.html" target="_blank" title="XDocument如何評論元素"> XDocument如何評論元素 </a> </li> <li> 7. <a href="http://hk.uwenku.com/question/p-dbyuhlix-v.html" target="_blank" title="如何從XDocument獲取NameTable?"> 如何從XDocument獲取NameTable? </a> </li> <li> 8. <a href="http://hk.uwenku.com/question/p-wzglijnt-vu.html" target="_blank" title="如何爲XDocument指定xmlns?"> 如何爲XDocument指定xmlns? </a> </li> <li> 9. <a href="http://hk.uwenku.com/question/p-tfpgfqpi-ex.html" target="_blank" title="如何在XDocument中放置Linq var?"> 如何在XDocument中放置Linq var? </a> </li> <li> 10. <a href="http://hk.uwenku.com/question/p-umiotbbp-zp.html" target="_blank" title="的XDocument不存在"> 的XDocument不存在 </a> </li> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-6208739752673518" data-ad-slot="4606349252"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <li> 11. <a href="http://hk.uwenku.com/question/p-esbvgnxv-ur.html" target="_blank" title="將XDocument優化爲XDocument XSLT"> 將XDocument優化爲XDocument XSLT </a> </li> <li> 12. <a href="http://hk.uwenku.com/question/p-cgycdoki-ho.html" target="_blank" title="如何訪問更深的xdocument節點?"> 如何訪問更深的xdocument節點? </a> </li> <li> 13. <a href="http://hk.uwenku.com/question/p-sxejgrqp-vk.html" target="_blank" title="如何遍歷XDocument的節點"> 如何遍歷XDocument的節點 </a> </li> <li> 14. <a href="http://hk.uwenku.com/question/p-flytycrz-baz.html" target="_blank" title="如何獲得XDocument的獨立副本?"> 如何獲得XDocument的獨立副本? </a> </li> <li> 15. <a href="http://hk.uwenku.com/question/p-comhyvld-bng.html" target="_blank" title="如何刪除XDocument中的XELEMENT?"> 如何刪除XDocument中的XELEMENT? </a> </li> <li> 16. <a href="http://hk.uwenku.com/question/p-psarccpa-th.html" target="_blank" title="如何訪問該的XDocument與XML LINQ"> 如何訪問該的XDocument與XML LINQ </a> </li> <li> 17. <a href="http://hk.uwenku.com/question/p-covpyqqt-bp.html" target="_blank" title="如何選擇使用的XDocument和LINQ"> 如何選擇使用的XDocument和LINQ </a> </li> <li> 18. <a href="http://hk.uwenku.com/question/p-owjmeufm-x.html" target="_blank" title="如何選擇XDocument中的項目?"> 如何選擇XDocument中的項目? </a> </li> <li> 19. <a href="http://hk.uwenku.com/question/p-qsbkxxrm-bcc.html" target="_blank" title="在XDocument之上的ViewModel"> 在XDocument之上的ViewModel </a> </li> <li> 20. <a href="http://hk.uwenku.com/question/p-rjqudcgt-gt.html" target="_blank" title="C#LINQ的XDocument"> C#LINQ的XDocument </a> </li> <li> 21. <a href="http://hk.uwenku.com/question/p-gzaomlhm-bgx.html" target="_blank" title="上的XDocument"> 上的XDocument </a> </li> <li> 22. <a href="http://hk.uwenku.com/question/p-rfyskyvf-em.html" target="_blank" title="解析的XDocument"> 解析的XDocument </a> </li> <li> 23. <a href="http://hk.uwenku.com/question/p-bmfrdwso-br.html" target="_blank" title="用的XDocument"> 用的XDocument </a> </li> <li> 24. <a href="http://hk.uwenku.com/question/p-epcfphnn-vd.html" target="_blank" title="的XDocument AncestorAndSelf"> 的XDocument AncestorAndSelf </a> </li> <li> 25. <a href="http://hk.uwenku.com/question/p-qdxfgrob-dd.html" target="_blank" title="從的XDocument"> 從的XDocument </a> </li> <li> 26. <a href="http://hk.uwenku.com/question/p-zuusersm-du.html" target="_blank" title="從的XDocument"> 從的XDocument </a> </li> <li> 27. <a href="http://hk.uwenku.com/question/p-sjelfboi-mg.html" target="_blank" title="替代的XDocument"> 替代的XDocument </a> </li> <li> 28. <a href="http://hk.uwenku.com/question/p-gdjdlamn-nz.html" target="_blank" title="XDocument/xNamespace"> XDocument/xNamespace </a> </li> <li> 29. <a href="http://hk.uwenku.com/question/p-pfgtcfcc-ey.html" target="_blank" title="如何通過XML的XDocument的XElement屬性在C#切換"> 如何通過XML的XDocument的XElement屬性在C#切換 </a> </li> <li> 30. <a href="http://hk.uwenku.com/question/p-tiqjgskd-bu.html" target="_blank" title="如何從XDocument獲取文本值?"> 如何從XDocument獲取文本值? </a> </li> </ul> </div> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-6208739752673518" data-ad-slot="1575177025"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <div class="padding-top-10"></div> </div> </div> <script type="text/javascript" src="http://img.uwenku.com/uwenku/script/side.js?t=1644592048261"></script> <script type="text/javascript" src="http://img.uwenku.com/uwenku/plugin/highlight/highlight.pack.js"></script> <link href="http://img.uwenku.com/uwenku/plugin/highlight/styles/docco.css" media="screen" rel="stylesheet" type="text/css" /> <script type="text/javascript"> $('pre').each(function(i, e) { hljs.highlightBlock(e, "<span class='indent'> </span>", false) }); </script> <div class="col-lg-3 col-md-4 col-sm-5"> <div id="rightTop"> <div class="row"> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-6208739752673518" data-ad-slot="5415218910" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <div class="row sidebar panel panel-default"> <div class="panel-heading font-bold"> 最新問題 </div> <div class="m-b-sm m-t-sm clearfix"> <ul class="side_article_list"> <li class="side_article_list_item"> 1. <a href="http://hk.uwenku.com/question/p-biqyvgby-bmt.html" target="_blank" title="openapi v3響應正文中的多行示例"> openapi v3響應正文中的多行示例 </a> </li> <li class="side_article_list_item"> 2. <a href="http://hk.uwenku.com/question/p-oepdilmp-bmn.html" target="_blank" title="Python 3.6.3 urlopen從URI中刪除服務器名稱以存儲在遠程服務器上的html文件"> Python 3.6.3 urlopen從URI中刪除服務器名稱以存儲在遠程服務器上的html文件 </a> </li> <li class="side_article_list_item"> 3. <a href="http://hk.uwenku.com/question/p-kdthjqxu-bnw.html" target="_blank" title="8位內聯彙編大小不匹配旋轉"> 8位內聯彙編大小不匹配旋轉 </a> </li> <li class="side_article_list_item"> 4. <a href="http://hk.uwenku.com/question/p-recklsrc-bnq.html" target="_blank" title="將字符串數組從WWW轉換爲Unity3d中的類列表"> 將字符串數組從WWW轉換爲Unity3d中的類列表 </a> </li> <li class="side_article_list_item"> 5. <a href="http://hk.uwenku.com/question/p-heqakphf-bnh.html" target="_blank" title="按鈕上的顏色隨時變化"> 按鈕上的顏色隨時變化 </a> </li> <li class="side_article_list_item"> 6. <a href="http://hk.uwenku.com/question/p-tjfkzchb-bcw.html" target="_blank" title="創建簡單的P2P網絡"> 創建簡單的P2P網絡 </a> </li> <li class="side_article_list_item"> 7. <a href="http://hk.uwenku.com/question/p-psejxepn-bdc.html" target="_blank" title="添加和更改網頁中的動態內容"> 添加和更改網頁中的動態內容 </a> </li> <li class="side_article_list_item"> 8. <a href="http://hk.uwenku.com/question/p-wkacljdr-bdn.html" target="_blank" title="字典分配"> 字典分配 </a> </li> <li class="side_article_list_item"> 9. <a href="http://hk.uwenku.com/question/p-ewahdfbq-bgy.html" target="_blank" title="基於彈簧配置文件的彈簧引導應用程序屬性"> 基於彈簧配置文件的彈簧引導應用程序屬性 </a> </li> <li class="side_article_list_item"> 10. <a href="http://hk.uwenku.com/question/p-chgwggio-bhu.html" target="_blank" title="NodeJs - 異步/待機異步/等待"> NodeJs - 異步/待機異步/等待 </a> </li> </ul> </div> </div> </div> <p class="article-nav-bar"></p> <div class="row sidebar article-nav"> <div class="row box_white visible-sm visible-md visible-lg margin-zero"> <div class="top"> <h3 class="title"><i class="glyphicon glyphicon-th-list"></i> 相關問題</h3> </div> <div class="article-relative-content"> <ul class="side_article_list"> <li class="side_article_list_item"> 1. <a href="http://hk.uwenku.com/question/p-zggfnylc-ng.html" target="_blank" title="如何過濾Xdocument並返回Xdocument?"> 如何過濾Xdocument並返回Xdocument? </a> </li> <li class="side_article_list_item"> 2. <a href="http://hk.uwenku.com/question/p-oyqicuty-sm.html" target="_blank" title="如何枚舉XDocument?"> 如何枚舉XDocument? </a> </li> <li class="side_article_list_item"> 3. <a href="http://hk.uwenku.com/question/p-olwoksgf-tp.html" target="_blank" title="如何使用的XDocument"> 如何使用的XDocument </a> </li> <li class="side_article_list_item"> 4. <a href="http://hk.uwenku.com/question/p-picxcvrg-bmv.html" target="_blank" title="如何改變用的XDocument"> 如何改變用的XDocument </a> </li> <li class="side_article_list_item"> 5. <a href="http://hk.uwenku.com/question/p-anljdhdr-bhc.html" target="_blank" title="xdocument添加xdocument"> xdocument添加xdocument </a> </li> <li class="side_article_list_item"> 6. <a href="http://hk.uwenku.com/question/p-uhpmgaeg-rz.html" target="_blank" title="XDocument如何評論元素"> XDocument如何評論元素 </a> </li> <li class="side_article_list_item"> 7. <a href="http://hk.uwenku.com/question/p-dbyuhlix-v.html" target="_blank" title="如何從XDocument獲取NameTable?"> 如何從XDocument獲取NameTable? </a> </li> <li class="side_article_list_item"> 8. <a href="http://hk.uwenku.com/question/p-wzglijnt-vu.html" target="_blank" title="如何爲XDocument指定xmlns?"> 如何爲XDocument指定xmlns? </a> </li> <li class="side_article_list_item"> 9. <a href="http://hk.uwenku.com/question/p-tfpgfqpi-ex.html" target="_blank" title="如何在XDocument中放置Linq var?"> 如何在XDocument中放置Linq var? </a> </li> <li class="side_article_list_item"> 10. <a href="http://hk.uwenku.com/question/p-umiotbbp-zp.html" target="_blank" title="的XDocument不存在"> 的XDocument不存在 </a> </li> </ul> </div> </div> </div> </div> </div> </div> </div><!-- wrap end--> <!-- footer --> <footer id="footer"> <div class="bg-simple lt"> <div class="container"> <div class="row padder-v m-t"> <div class="col-xs-8"> <ul class="list-inline"> <li><a href="http://hk.uwenku.com/contact">聯系我們</a></li> <li>© 2020 HK.UWENKU.COM</li> <li><a target="_blank" href="https://beian.miit.gov.cn/">沪ICP备13005482号-4</a></li> <li><script type="text/javascript" src="https://v1.cnzz.com/z_stat.php?id=1280101193&web_id=1280101193"></script></li> <li><a href="http://www.uwenku.com/" target="_blank" title="优文库">简体中文</a></li> <li><a href="http://hk.uwenku.com/" target="_blank" title="優文庫">繁體中文</a></li> <li><a href="http://ru.uwenku.com/" target="_blank" title="поле вопросов и ответов">Русский</a></li> <li><a href="http://de.uwenku.com/" target="_blank" title="Frage - und - antwort - Park">Deutsch</a></li> <li><a href="http://es.uwenku.com/" target="_blank" title="Preguntas y respuestas">Español</a></li> <li><a href="http://hi.uwenku.com/" target="_blank" title="कार्यक्रम प्रश्न और उत्तर पार्क">हिन्दी</a></li> <li><a href="http://it.uwenku.com/" target="_blank" title="IL Programma di chiedere Park">Italiano</a></li> <li><a href="http://ja.uwenku.com/" target="_blank" title="プログラム問答園区">日本語</a></li> <li><a href="http://ko.uwenku.com/" target="_blank" title="프로그램 문답 단지">한국어</a></li> <li><a href="http://pl.uwenku.com/" target="_blank" title="program o park">Polski</a></li> <li><a href="http://tr.uwenku.com/" target="_blank" title="Program soru ve cevap parkı">Türkçe</a></li> <li><a href="http://vi.uwenku.com/" target="_blank" title="Đáp ứng viên">Tiếng Việt</a></li> <li><a href="http://fr.uwenku.com/" target="_blank" title="Programme interrogation Park">Française</a></li> </ul> </div> </div> </div> </div> </div> </footer> <!-- / footer --> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?f78a970f17b19a79fc477a3378096f29"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> </body> </html>