Forum

Create an xml element that has int32 values

Hi,
I’m trying to make an XML doc using numerous Xelements, but I noticed it isn’t possible to make an element any value other than a string

In the image above is example of the result of using ToJson with my current XNode Tree, I would like the year to be presented as an integer, that is, without the “quote” marks.

Other than editing the string after the fact which will have thousands of entries eventually, is there a way to set the vaule type ahead of time?

Encountered this issue as well, and I don’t think that’s possible using the default XML nodes. Maybe using Newtonsoft.JSON helps there?

edit: just saw that on SO

edit2: also, looking at the .net6 documentation on JSON serialization, it looks like it’s able to serialize JSON with something else than strings. not sure how that’ll apply to the 2022 branch tho.

1 Like

Not quite sure, but I think you have to either add a datatype attribute to your elements or define a XML schema that assigns the datatypes to the elements. Ofc this will only work if the parser used by ToJSON supports that.
Further Info.

1 Like

that’s what I had in mind yeah, did something similar when needed to enforce JSON arrays on serialized output. guess there should be a similar XML attribute that newtonsoft “understands” and converts to the type you want.

1 Like

Briefly checked the XElement nodes and they appear to be based on System.Xml.Linq and the XElement.Value property type is defined as string.

1 Like

@Hadasi 2021.4.10 includes a bunch of new (experimental at this point) json and xml nodes that might help:

  • JSONReader (Generic)
  • JSONWriter (Generic)
  • JSONToObject
  • ObjectToJSON

these work together with jsontypedef. where you need to do these manual steps:

  • write a json schema
  • use jtd-codegen to generate c# out of it
  • build that c# code into a .dll which you then reference in your document

from then you can use the above nodes.

similar for XML:

  • XMLReader (Generic)
  • XMLWriter (Generic)
  • XMLToObject
  • ObjectToXML

these work together with xsd.

2 Likes

Is that supposed to work with 2021.4.10?

It looks like the generated C# can only rely on System.Text.JSON which only seems to be available in >= net5.0, hence not compatible with the .4 branch of vvvv.

edit : maybe other JSON to C# things would do the trick, let’s see…

edit2: ok now I’m super confused : internally JSONReader (Generic) indeed uses System.Text.Json.dll, the nuget page shows it’s compatible with netstandard2.0, but Microsoft’s documentation tells that " The requested page is not available for .NET Standard 2.0. You have been redirected to the newest product version this page is available for." and redirects me to the net6.0

edit3: ok, looks like one explicitely has to install System.Text.JSON inside Visual Studio for it to work. jesus that .net ecosystem is soooo confusing… managed to build a dll in the end, yuhu!

yes, with the experimental nodes mentioned above

yep, was more confused about VS not able to load System.Text.JSON out of the box and the MS documentation telling that this does not exist in netstandard2.0… doh.

edit: works like a charm. small add though : if one wants to generate a working JSON schema from some existing JSON, they’d have to use jtd-infer.