how to save serializable object to database field - linq-to-sql

I'm using linq to sql and I have the need to save a few of our serializable objects to a field in our sql server database. I've defined the field as varbinary(MAX) but I'm not 100% sure that's correct. So, does anyone know how I can save an object to this field and then read it back? Thanks.

I think this depends on the format the objects are serialized in. For instance, if you are serializing out to xml then sql server (at least the latest version) supports an xml type. If you are indeed serializing out to binary, then sticking with binary is probably okay. Picking a db type that is similar to the type of the serialization will minimize a transformation bridging the db type to the object type.

I have gotten the object to serialize but I'm running into a problem getting the resulting xml saved. The object field is defined as xml so in linq to sql it's an xelement. I'm using the following code to serialize and get the xml string. But when I try to load the xml string to the xelement I get the "Illegal characters in path" error. However when I look at the generated xml I don't see any illegal characters. Here is the generated xml.
using (StringWriter sw = new StringWriter())
{
var x = new XmlSerializer(sessionObject.ObjectToStore.GetType());
x.Serialize(sw, sessionObject.ObjectToStore);
sessionRecord.sessionObject = XElement.Load(sw.ToString());
}
<?xml version="1.0" encoding="utf-16"?>
<SerializableJobSearch xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Keywords>electronics</Keywords>
<StateList />
<Radius>50</Radius>
<City />
<State />
<DisciplineIdList />
<IndustryIdList />
<Direct>false</Direct>
<TempHire>false</TempHire>
<Contract>false</Contract>
<PerHour>false</PerHour>
<PerYear>false</PerYear>
<DegreeLevel />
<IncludeJobsRequiringLess>false</IncludeJobsRequiringLess>
<AddedWithin>0</AddedWithin>
<OrderBy>Rank</OrderBy>
<OrderByRank>true</OrderByRank>
<resultsPerPage>50</resultsPerPage>
<NeedToMake />
</SerializableJobSearch>

Related

Converting json object with variable attributes into xml array

The Api response is {"Content":{"634331":["Product could not be found"],"634332":["Product could not be found"],"etc…
and I am having trouble to catch the values after content into xml :
<Content>
<__634331>Product could not be found</__634331>
<__634332>Product could not be found</__634332>
<__123104398>Product could not be found</__123104398>
The values are being interpreted as field names.
Is there a way to convert json object into xml array looking like:
<Content>
<res>
<key>634331</key>
<value>Product could not be found</value>
</res>
<res>
<key>634332</key>
<value>Product could not be found</value>
</res>
It's quite common in JSON for keys in a map (or "object") to represent data values rather than property names. Unfortunately this doesn't map at all well to XML, where the equivalent would usually be a structure like
<data key="__634331" value="Product could not be found"/>
No automatic converter is going to be able to recognise that this kind of conversion is appropriate.
My recommendation would be to do a custom conversion using XSLT 3.0 template rules. I would need to see more detail of your JSON and required XML to advise in more detail.

Orbeon autocomplete display JSON response

I'm trying to fill the autocomplete field in Orbeon (version 2016.1) with suggestions which I receive as a JSON.
The JSON I get looks like:
{"status":"success","code":200,"data":{"streets":[{"name":"Street One","id":"1"},{"name":"Street Two","id":"2"},{"name":"Street Three","id":"3"}]}}
I know that the Resource URI should point to my web service (could that URI, or the arguments I need to send, be encoded?), but I don't know how the Items, Label and Value fields should be configured in this case (the label would be name from the json and value should point to the code from the json, of course).
I referred to https://doc.orbeon.com/xforms/submission-json.html but haven't exactly managed to get what I'm trying to.
Can someone help?
Thanks in advance.
Masa
In particular, with your specific JSON, the corresponding XML will look as follows. In general, see the section Seeing the converted XML for how you can create a form in Form Builder that allows you to see what the converted XML is for any JSON.
<json type="object">
<status>success</status>
<code type="number">200</code>
<data type="object">
<streets type="array">
<_ type="object">
<name>Street One</name>
<id>1</id>
</_>
<_ type="object">
<name>Street Two</name>
<id>2</id>
</_>
<_ type="object">
<name>Street Three</name>
<id>3</id>
</_>
</streets>
</data>
</json>

How can Prestashop 1.5 Webservice can return json format instead of xml format

I want the webservice to return JSON format instead of the default XML. What should I do?
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<customers>
<customer id="1" xlink:href="http://www.mywebsite.com/api/customers/1"/>
<customer id="2" xlink:href="http://www.mywebsite.com/api/customers/2"/>
</customers>
</prestashop>
This is what I am getting when I entered http://www.mywebsite.com/api/customers in the address bar. This is XML format. I need to convert this into JSON.
It seems so late to reply this question but for others that might help.
You just need to append the param ?output_format=JSON to force Prestashop to return JSON data instead of XML by default.
E.g http://www.mywebsite.com/api/customers?output_format=JSON
This is a recurrent question. Initially Prestashop web services come with XML format. Then the JSON format was added only afterward.

how to get value From XML object in action script

I am new to flex development. I am trying to develop a web based application. somehow I feel comfortable. I have some queries related to xml documents.
In my webservice I perform some database operation and return some xml data in XmlDocument Object format.
I tried to parse it using below function
public function objectToXML1(obj:Object):XML {
var qName:QName = new QName("DATA");
var xmlDocument:XMLDocument = new XMLDocument();
var simpleXMLEncoder:SimpleXMLEncoder = new SimpleXMLEncoder(xmlDocument);
var xmlNode:XMLNode = simpleXMLEncoder.encodeValue(obj, qName, xmlDocument);
var xml:XML = new XML(xmlDocument.toString());
return xml;
}
but I don't know how to pull data from this XML
XML FORMAT
<REQUEST>
<STATUS>SUCCESS</STATUS>
<MESSAGE>BP Retrive </MESSAGE>
<DATA>
<BOM>
<BO>
<BusinessPartners>
<row>
<CardCode/>
<CardName/>
<Phone1/>
<Phone2/>
<Cellular/>
<EmailAddress/>
</row>
</BusinessPartners>
</BO>
</BOM>
</DATA>
</REQUEST>
If you have a predefined structure of the XML, you could use FlexXB to create an ActionScript Object of the XML. It is pretty easy, you create an ActionScript Object which looks exactly like the XML (that means it need to have the same properties) and you annotate these properties. So FlexXB will read the XML and create an ActionScript Object form it.
If you don't want to use the framework, you can do it by hand too. To read attributes, use xmlElement.#attributeName for getting the name of an object use e.g. xmlElement.name().localName. There are some functions, but since the autocompletion doesn't work for XML elements it is a little hard to find the right functions. Here is an article by Adobe which explains how to work with XML. Basicall you can just use the node names as property names. In your case you could write xmlElement.Phone1 and so on.
Hope it helps :)

rails mysql BLOB type - to_xml returns binary, would like to force to string

I am running RoR, Ruby 1.8.6/Rails 2.3.2.
I am persisting a String within a model object to MySQL to a Blob field. For a number of reasons, I have to use MySQL Blob data type instead of a Text data type, yet I want Rails to treat the field as a regular String (instead of a binary String - which is what I think it is doing?).
How can I force this? Everything works well in the display of the pages, the main issue is XML that is being produced.
A specific example is a "description" field of type Blob in MySQL displays perfectly in the HTML ERB, but when using the to_xml method on that model object the resulting XML is:
<description type="binary" encoding="base64">
VGhpcyB0d28tc2NyZWVuIGZvcm0gYWxsb3dzIGJ1c2luZXNzIGN1c3RvbWVy
cyB0byByYXRlIHRoZWlyIGxldmVsIG9mIHNhdGlzZmFjdGlvbiBhbmQgcmVx
dWVzdCB0byBzcGVhayB3aXRoIG1hbmFnZW1lbnQuIEl0IGlzIGVzcGVjaWFs
bHkgZGVzaWduZWQgZm9yIHRoZSBDb21taXNzaW9uIG9uIFZvY2F0aW9uYWwg
UmVoYWJpbGl0YXRpb24u
</description>
I just want it to appear be formatted as a normal string field like:
<description>test description</description>
What are the best ways to force this? I've been searching and reading through docs, but haven't been finding any helpful suggestions.
I appreciate any and all help,
thank you
I ended up monkey patching the XmlSerializer to treat binary data as text. I will never have to send binary data down in pure binary form via XML, so this was a valid option - not perfect, but in doing research couldn't find any way to override the default column settings.