2014-09-30 122 views
0

我使用JSON.Net更新XML文件中的對象。這很好用!但是,我需要更新一個屬性,其中包含一些特殊的字符,並且我無法找到有關如何使用JSON.Net執行此操作的任何信息。參考對象屬性,當屬性中包含特殊字符

XML

<ProfileSettings>{ 
    "Name": "Default", 
    "Status": "New", 
    "DeploymentVariants": { 
    "SPSite": { 
     "Signature": "SPSite", 
     "Type": "SPSite", 
     "Label": "SharePoint Site", 
     "Source": "Solution", 
     "DefaultValue": "http://google.com", 
     "Value": "http://google.com" 
    }, 
    "Process/Participant[@Name=\"Manager\"]&gt;User": { 
     "Signature": "Process/Participant[@Name=\"Manager\"]&gt;User", 
     "Type": "SPUser", 
     "Label": "User for swim lane Manager", 
     "Source": "Swimlane", 
     "DefaultValue": "John Smith", 
     "Value": "John Smith" 
    } 
    }, 
    "DeploymentScripts": {}, 
    "SPList": "mySPList" 
}</DeploymentProfile> 

爲了在SPSite對象更新默認值,我可以使用JSON.Net像這樣:

dynamic fromSolution = JsonConvert.DeserializeObject(profileObject); 
fromSolution.DeploymentVariants.SPSite.DefaultValue = txtSPSite.Text; 

然而,這不會是如果屬實我試圖訪問過程/參與者[@Name = \「Manager \」] >用戶對象。如果有特殊字符,如何訪問這個屬性?

fromSolution.DeploymentVariants.Process.DefaultValue沒有工作,顯然包括內的特殊charecters只會導致運行錯誤。

回答

0

JObject實現IDictionary<string, object>。你可以使用字典語法:

fromSolution.DeploymentVariants["Process/Participant[@Name=\"Manager\"]>User"].DefaultValue 
+0

謝謝Athari,這個作品!然而,有沒有更好的方式來做到這一點,因爲@ @ = =「經理」可以被稱爲別的... – mwilson 2014-09-30 20:49:50

+1

@mwilson然後枚舉'JObject'的屬性。尋找名爲'Property'或類似的東西(我不記得確切的名字)的屬性和方法。我會質疑你的配置設計。如果你必須這樣做,你會犯錯誤。 – Athari 2014-09-30 20:52:28

+0

是的,不是我的設計。必須與我所得到的一起工作。感謝您的幫助! – mwilson 2014-09-30 21:05:29