简体   繁体   中英

At @ sign in property of dynamic object

Im parsing a XML document

      <book id="bk102">
          <author>Ralls, Kim</author>
......
.....
.....
       </book>

to Json and from Json to a dynamic object like this.

   string jsonText = JsonConvert.SerializeXNode(xml);
        dynamic dyn = JsonConvert.DeserializeObject<ExpandoObject>(jsonText);
        var catalog = dyn.catalog;

It works an i can get the child elements like book.author

however when i watch the dynamic object in visual studio i see that the XML elements attribute "id" has been parsed into a property called @id. But i cant access it either with book.id or book.@id. None of them seem to exist.

How is @id properties accessed?

In C#, @ is a special character .

In order to access properties named with @ at the beginning, you can use an indexer, eg:

var id = ((IDictionary<string, object>)book)["@id"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM