Arian Motamedi
Arian Motamedi

Reputation: 7423

Json.Net Serialize Complex Object to Xml Attribute And Value

Let's say I have a Json object that looks like this:

{
    "Phones": [
        {
            "Phone": {
                "Value": 123,
                "@Type": "Foo"
            }
        }
    ]
}

I want to call JsonConvert.DeserializeXmlNode() but would like the resulting XML to look like this:

<Phones>
    <Phone Type="Foo">123</Phone>
</Phones>

Currently Value is being deserialized to an xml element as a child of Phone, but I want it to be the XML value of Phone. Is there a way to do this using Json.Net, like a special operator that tells it to deserialize it as such, without having to create a custom serializer? Any help is apppreciated.

Upvotes: 2

Views: 1102

Answers (1)

Arian Motamedi
Arian Motamedi

Reputation: 7423

I just figured it out. using

"Phone": {
      "@Type": "Foo",
      "#text": 123
 }

gives me the expected result. #text tells it not to create a child element for that value.

Upvotes: 3

Related Questions