We have a Newtonsoft.Json library and two classes.

Class 1:

 Public Class SomeClass Private _some_property As String Public Property SomeProperty() As String Get Return _some_property End Get Set(ByVal value As String) _some_property = value End Set End Property End Class 

Class 2:

 Imports Newtonsoft.Json Public Class SomeWrapper Private _items As IList(Of SomeClass) = New List(Of SomeClass) <JsonProperty("Wrapper")> Public Property Items() As IList(Of SomeClass) Get Return _items End Get Set(ByVal value As IList(Of SomeClass)) _items = value End Set End Property End Class 

And the following client code:

  Dim someObj As New SomeClass Dim someWrap As New SomeWrapper someObj.SomeProperty = "blablabla" someWrap.Items.Add(someObj) someWrap.Items.Add(someObj) someWrap.Items.Add(someObj) Dim json As String = JsonConvert.SerializeObject(someWrap, Formatting.Indented) Dim node As XNode = JsonConvert.DeserializeXNode(json, "root") Dim result As String = node.ToString 

In the json variable we have:

 { "Wrapper": [ { "SomeProperty": "blablabla" }, { "SomeProperty": "blablabla" }, { "SomeProperty": "blablabla" } ] } 

In the result variable, we have:

 <root> <Wrapper> <SomeProperty>blablabla</SomeProperty> </Wrapper> <Wrapper> <SomeProperty>blablabla</SomeProperty> </Wrapper> <Wrapper> <SomeProperty>blablabla</SomeProperty> </Wrapper> </root> 

The question is how to designate the properties of an object so that they are defined in JSON as XML attributes. That is, SomeProperty should become XML attributes and not nodes.

  • for an example of an exit there is also an example of an input to add - Grundy
  • @Grundy gave a detailed example. - Dmitry Gvozd

1 answer 1

Please note: in the JsonConvert.DeserializeXNode(json, "root") your class does not participate in any side, there is a direct conversion of the string to XNode.

Therefore, the answer - no way. At least without changing the JSON format.


If you have a question - how to change the JSON format so that SomeProperty becomes an attribute - try to do the opposite. Collect the XML you need and try to convert it to JSON.

Most likely, the <JsonProperty("@SomeProperty")> attribute will help

If you don’t need JSON at all - forget Newtonsoft.Json and use the XmlSerializer :

 var serizliaer = new XmlSerializer(typeof(SomeWrapper)); var doc = new XDocument(); using (var writer = doc.CreateWriter()) serializer.Serialize(someWrap, writer); var node = doc.Root; node.Remove(); 
  • Most please do not overfill from a sieve. No need to give up anything and no need to change technology. The question is how to use this particular library to get XML from objects described above and so that SomeProperty becomes an attribute. In your example there is no achieved result. - Dmitriy Gvozdi
  • @DmitryI quote: "Therefore, the answer is no way." - Pavel Mayorov
  • In the Newtonsoft.Json library Newtonsoft.Json there is an opportunity to change JSON but I can’t find an example in the documentation and could not, I don’t have enough knowledge of English - Dmitry Gvozdi
  • @ DmitriyGvozd so you have the JSON format set - or is it changing? If given, then nothing. If it changes, I answered what can be done. - Pavel Mayorov
  • For example, I know how to declare a type for an object, but I don `t know how to set attributes. - Dmitry Gvozd