如何映射此XmlDocument
的每个<?xml version="1.0" encoding="UTF-8"?>
<RESULT>
<TAG ID="foo">Hello</TAG>
<TAG ID="bar">World</TAG>
... (huge amount of tags)
<RESULT>
代码,而不明确指出所有成员映射:
public class Result
{
public string Foo { get; set; }
public string Bar { get; set; }
... (huge amount of properties)
}
上课:
$(document).ready(function(){
// Search Submit
$("#search").submit(function(event){
event.preventDefault("", function(){
//
});
name = $("#search-name").val();
console.log("Search term at submit is: "+name);
// Send to Google Geocoding API
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
address : name
}, function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
console.log("results after query: "+ results.formatted_address + "\n" + results.length + " QTY Posts");
}
);
});
});
答案 0 :(得分:2)
您可以这样做:
class ValueComparator implements Comparator<String> {
Map<String, Integer> map;
public ValueComparator(final Map<String, Integer> map) {
this.map = map;
}
@Override
public int compare(final String keyA, final String keyB) {
final Integer valueA = this.map.get(keyA);
final Integer valueB = this.map.get(keyB);
System.out.println(keyA + " keyA ");
System.out.println(keyB + " keyB ");
final int compared = valueA.compareTo(valueB);
if (compared != 0) {
return compared;
} else {
return keyA.compareTo(keyB);
}
}
}
请注意,您可以决定使用其他方法在AutoMapper.Mapper.CreateMap<XmlDocument,Result>()
.ForAllMembers(opt => opt.ResolveUsing(res =>
{
XmlDocument document = (XmlDocument)res.Context.SourceValue;
var node = document
.DocumentElement
.ChildNodes
.OfType<XmlElement>()
.FirstOrDefault(
element =>
element
.GetAttribute("ID")
.Equals(res.Context.MemberName, StringComparison.OrdinalIgnoreCase));
if (node == null)
throw new Exception("Could not find a corresponding node in the XML document");
return node.InnerText;
}));
内找到合适的节点。例如,您可能决定使用XPath。
另请注意,如果在XmlDocument
中找不到相应的节点,则会抛出异常。在这种情况下,您可能决定做其他事情。例如,您可能决定返回null,空字符串或某个默认值。