[英]XMLDocument: Read XDocument and put contents into other XDocument (LINQ)(C#)
使用LinQ,我在XDocument machine2.config中具有以下內容
<appSettings>
<add key="FreitRaterHelpLoc" value="" />
<add key="FreitRaterWebHome" value="http://GPGBYTOPSPL12/" />
<add key="FreitRaterDefaultSession" value="" />
<add key="FreitRaterTransferMode" value="Buffered" />
<add key="FreitRaterMaxMsgSize" value="524288" />
<add key="FreitRaterMaxArray" value="16384" />
<add key="FreitRaterMaxString" value="32768" />
<add key="FreitRaterSvcTimeout" value="60" />
</appSettings>
我需要在XDocument machine.config中的特定位置獲取它
我首先手動對元素進行了硬編碼,然后XPathSelectElement()。AddAfterSelf()可以很好地工作以將XML塊獲取到我想要的位置。 但是我需要從文件中讀取它,以便可以通過快速編輯文檔來更改它。 現在,用這段代碼
XDocument doc = XDocument.Load("machine.config");
XDocument AppSettings = XDocument.Load("machine2.config");
string content = AppSettings.ToString();
doc.XPathSelectElement("configuration/configSections").AddAfterSelf(content); //need it to insert after </configSections>
我可以添加它,但是它不能以正確的格式復制。 而是在machine.config中,我得到了
</configSections><appSettings><add key="FreitRaterHelpLoc" //etc
什么時候應該
</configSections>
<appSettings>
...
無論如何,我都很迷茫,所以如果有人知道一種以正確格式將文件從一個文件傳輸到另一個文件的方法,我將不勝感激。
將您的代碼更改為
XDocument doc = XDocument.Load("machine.config");
XElement AppSettings = XElement.Load("machine2.config"); //NB: not XDocument
doc.XPathSelectElement("configuration/configSections").AddAfterSelf(AppSettings);
doc.Save("machine.config"); //Of course I'm sure you had this line somewhere
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.