当属性具有特殊字符时,使用JSON.Net引用对象属性

时间:2014-09-30 20:41:30

标签: c# xml json json.net

我正在使用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对象中的DefaultValue,我可以像这样使用JSON.Net:

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

但是,如果我尝试访问进程/参与者[@Name = \&#34; Manager \&#34;]&gt;用户对象,则不会成为现实。如果属性有这样的特殊字符,我如何访问该属性?

fromSolution.DeploymentVariants.Process.DefaultValue无效,显然包含特殊字符只会导致运行时错误。

1 个答案:

答案 0 :(得分:0)

JObject实施IDictionary<string, object>。您可以使用字典语法:

fromSolution.DeploymentVariants["Process/Participant[@Name=\"Manager\"]>User"].DefaultValue
相关问题