Linq to XML customize distinct function
Definition comparer class,
class ItemComparer : IEqualityComparer<XElement>
{
public bool Equals(XElement x, XElement y)
{
return x.Attribute("Name").Value == x.Attribute("Name").Value;
}
public int GetHashCode(XElement obj)
{
return obj.Attribute("Name").Value.GetHashCode();
}
}
How to use this one to distinct:
public List<string> LoadDistrict(string region)
{
if (m_DataDoc == null)
return null;
List<string> list = new List<string>();
try
{
var query = from r in m_DataDoc.Elements("DashBoardData").Elements("LevelData").Elements("Region").Where(a => a.Attribute("Name").Value.ToLower() == region.ToLower())
select r;
foreach (var district in query.Elements("District").Distinct(new ItemComparer()))
{
list.Add(district.Attribute("Name").Value);
}
return list;
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
return null;
}
}
Ïà¹ØÎĵµ£º
1¡¢µ¼³öµ½XMl select * from Brand for xml auto ,root('Brands')
<Brands>
<Brand BrandID="E584596D-4D66-4F2F-B6F7-71C3BEB4CA21" Name="inganico" />
<Brand BrandID="19B04451-DDC4-4CDF-BE30-CB4E703B27DA" Name="°²¸¶´ï" />
<Brand BrandID="3C6C8E12-7C4A-4F1 ......
private ClassLoader xmlLoader = Thread.currentThread().getContextClassLoader();
private void parseXML(String resource){
resource = StringUtils.substring(resource,resource.indexOf(":")+1,resource.length());
InputStream inputStream = xmlLoader.getResourceAsStream(resou ......
Java codeFormat ft=Format.getRawFormat();
ft.setEncoding("utf-8");
ft.setIndent(" ");//ÉèÖÃËõ½ø
XMLOutputter outputter=new XMLOutputter(ft);//ÉèÖÃXML¸ñʽ
Õâλ˵µÄ²»´í£»
ÔÚieÀïÃ棬ËüÄÜ°Ñxml×Ô¶¯½âÎö³ÆÊ÷Ðνṹ£¬ËùÒÔ¿´ÆðÀ´Ã»ÎÊÌ⣬ÔÚ¼Çʱ¾ÀïÃ棬¾Í²»ÐÐÁ ......
Android ÊÇÕë¶ÔÒƶ¯É豸µÄÒ»ÖÖÐÂÐ˵ĿªÔ´²Ù×÷ϵͳºÍ SDK¡£½èÖúËü£¬Äú¿ÉÒÔ´´½¨¹¦ÄÜÇ¿´óµÄÒƶ¯Ó¦ÓóÌÐò¡£µ±ÄúµÄÓ¦ÓóÌÐò¿ÉÒÔ·ÃÎÊ Web ·þÎñʱ£¬ÆäÎüÒýÁ¦»á´ó´óÔö¼Ó£¬ÕâÒâζ×ÅÄúÐèҪʹÓà Web ÓïÑÔ£ºXML¡£ÔÚ±¾ÎÄÖУ¬Äú½«Á˽âÔÚ Android ÉÏʹÓà XML µÄ²»Í¬·½·¨£¬ÒÔ¼°ÈçºÎʹÓÃËüÃǹ¹½¨×Ô¼ºµÄ Android Ó¦ÓóÌÐò¡£
ÈëÃÅ
ÔÚ±¾ÎÄÖÐ ......