How to ignore type level in xml by XmlSerializer - xml-serialization

I have class like this:
(I know DataInLoop and TwoColumnData have the same properties, plz ignore that as that's not the problem.)
I made a helper function to serialize this DataList class to xml:
the xml result is like this:
I don't want LoopData level, is it possible to remove that level?
What I want is like this:
<DataList>
<Data>....</Data>
<DataInLoop>...</DataInLoop>
<DataInLoop>...</DataInLoop>
</DataList>

Just add XmlElementAttribute to your list
[XmlElement("DataInLoop")]
public List<DataInLoop> LoopData { get;set; }

Related

use GwtCreateResource to provide text programatically

I would like my uiBinder to use a ClientBundle which will provide some runtime customized labels. Kind of a TextResource but not from a text file !
I tried with GwtCreateResource but from the DevGuide it seems like it's not possible. Am I right ? (create() and name() are the only methods available)
What I would like to achieve is something like this:
client bundle:
public interface MyWidgetResources extends ClientBundle {
GwtCreateResource<WidgetLabels> labels();
#Source("lol.css")
CssResource style();
}
labels class:
public final class MyWidgetLabels {
public String title() {
return load("mywidget-title");
}
public String banner() {
return load("mywidget-banner");
}
private String load(String key) {
// load from external..
}
}
uiBinder:
<ui:with type="com.package.MyWidgetResources" field="res"/>
<gwt:SimplePanel>
<gwt:Label text="{res.labels.title}"></gwt:Label>
<gwt:Label text="{res.labels.banner}"></gwt:Label>
</gwt:SimplePanel>
My code looks like this already but res.label.title does not work because GwtCreateResource can only serve as class instantiator (res.labels.create().title()).
Is there a solution for me ? Maybe with a custom ResourceGenerator ?
As long as MyWidgetLabels can be created by GWT.create, you can put anything you want into that type, and you can make it behave however you'd like. You will need the create reference in your uibinder as you suggested at the end of the post to actually build the object, so your lines will look about like this:
<gwt:Label text="{res.labels.create.title}"></gwt:Label>
Each . separated piece (except the first, which is a ui:field/#UiField) is a no-arg method to be called - you declared labels() in MyWidgetResources, create() already existed in GwtCreateResource, and you created title() in your own MyWidgetLabels type.
Since that first piece is a ui:field/#UiField, you could have another that references res.labels.create as something like labels so that later you could instead say:
<gwt:Label text="{labels.title}"></gwt:Label>
Finally, yes, you could build your own ResourceGenerator which would enable you to do whatever you wanted to emit the type in question, as long as you extended the ResourcePrototype type and had a getName() method.

referencing another field in a dozer custom field converter

I have 2 classes with this structure:
class ClassA {
String typeA;
List<String> valuesA;
... other fields go here
}
class ClassB {
String typeB;
List<String> valuesB;
... other fields go here
}
I need to map the valuesA to valuesB and in the custom coverter for this field mapping I need to access the typeA/typeB.
Something like: if typeA is "type1" then map valuesA to valuesB by some algorithm and if typeA is "type2" then map by a different algorithm the valuesA to valuesB.
Is this possible with Dozer?
Thanks!
I think it is possible by using the programmatic (ie in Java) custom converters of Dozer.
See the documentation, especially the TestCustomConverter Java class for an example.

Instruct XmlSerializer to process serialized/deserialized data?

I have an enum property. I want the serialized XML for this property to be the splitted camel-case string of the enum and vice versa.
I have two functions, one is ConcatCamelCase and the other is SplitCamelCase, I want the serializer to use them accordingly, is this possible by just decorating the field with an attribute?
If no, what are the other option without having to mess with all the other fields?
You'll have to do something like this:
public class SomeClass {
[XmlIgnore]
public MyEnum MyRealProperty {get;set;}
[XmlElement("MyRealProperty")]
[EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
public string MyProxyProperty
{
get {return SplitCamelCase(MyRealProperty);}
set {MyRealProperty = ConcatCamelCase(value);}
}
}
You can explicitly set the name of everything that is serialized using the XMlSerialization attributes.
[XmlRoot("theNameYouWant")]
[XmlElement("theNameYouWant")]

Custom sort in domaincollectionview

I'm using a DCV as a property in the View Model.
Everything works fine but what about custom sort?
Say I have a string property in my model which should be sorted alphanumerically.
How can I achieve such thing?
UPD:
Model:
public class MyModel
{
///...
public SomeProperty {get;set;}
}
xaml:
<data:DataTextColumn Binding={binding path=SomeProperty}, canusersort=true />
When sorting within the datagrid, the property gets sorted with disregard to alphanumeric order, i.e. in a regular string way. I'd like to apply my custom sort, e.g. by introducing my own IComparer. No API is available at least as I know of it.
Any clues?
The DomainCollectioView has special collection:
SortDescriptions
You could add next code in Your ViewModel:
DCV.SortDescriptions.Add(new SortDescription("SomeProperty ", ListSortDirection.Ascending));

Handling MVC2 variables with hyphens in their name

I'm working with some third-party software that creates querystring parameters with hyphens in their names. I was taking a look at this SO question and it seems like their solution is very close to what I need but I'm too ignorant to the underlying MVC stuff to figure out how to adapt this to do what I need. Ideally, I'd like to simply replace hyphens with underscores and that would be a good enough solution. If there's a better one, then I'm interested in hearing it.
An example of a URL I want to handle is this:
http://localhost/app/Person/List?First-Name=Bob&My-Age=3
with this Controller:
public ActionResult List(string First_Name, int My_Age)
{
{...}
}
To repeat, I cannot change the querystring being generated so I need to support it with my controller somehow. But how?
For reference, below is the custom RouteHandler that is being used to handle underscores in controller names and action names from the SO question I referenced above that we might be able to modify to accomplish what I want:
public class HyphenatedRouteHandler : MvcRouteHandler
{
protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
{
requestContext.RouteData.Values["controller"] = requestContext.RouteData.Values["controller"].ToString().Replace("-", "_");
requestContext.RouteData.Values["action"] = requestContext.RouteData.Values["action"].ToString().Replace("-", "_");
return base.GetHttpHandler(requestContext);
}
}
Have you tried [Bind(Prefix="First-name")]? It might work...
One way would be with a custom model binder. Another way would be with an action filter. Use the model binder if you want to do this on a specific type. Use the action filter if you want to do this on a specific action or controller. So for the latter method you could do something like:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var keys = filterContext.HttpContext.Request.QueryString.AllKeys.Where(k => k.Contains('-'));
foreach(var k in keys)
{
filterContext.ActionParameters.Add(
new KeyValuePair<string, object>(
k.Replace('-', '_'), filterContext.HttpContext.Request.QueryString[k]));
}
base.OnActionExecuting(filterContext);
}
I had the same problem. In the end rather than doing something too complex I just get the query string parameters using
string First_Name = Request["First-Name"];
You may want to check for NUlls incase the parameter is not there, but this sorted it out for me. You can also include an optional parameter for the ActionResult for test purposes etc..