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;
}
}
Ïà¹ØÎĵµ£º
ʹÓÃXmlDocumentÀàÍê³É¶ÔXMLµÄ²é¡¢É¾¡¢Ìí¡¢¸Ä
http://www.aspdiy.net/article/53.htm
ºǫ́C#´úÂë
1using System;
2using System.Collections;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System. ......
XML¿ª·¢ÈëÃÅ»ù´¡£ºXMLÓï·¨¹æÔò(2) - ÍøÒ³Éè¼Æ×¨À¸ - ±à³ÌÈëÃÅÍø
http://www.bianceng.cn/web/XML/200911/11986_2.htm
×Ö·û ʵÌåÒýÓÃ
> &n ......
ͨ³£Çé¿öÏÂ,ÔÚJAX-WS2.0ÖÐÊÇÀûÓÃJAXB½«´«ÈëµÄSOAP XMLÏûϢת»»³ÉJAVAµÄobject,È»ºóµ÷ÓöÔÓ¦porttypeµÄJAVAÀàµÄÏàÓ¦µÄ·½·¨,ÔÚÍê³Éµ÷Óúó,ÔÙͨ¹ýJAXB½«·µ»Ø²ÎÊýת³ÉSOAP XML.
JAX-WS»¹ÌṩÁËÁíÒ»ÖÖ·½·¨,ÈÃÎÒÃÇ¿ÉÒÔÖ±½Ó²Ù×÷SOAP XML,ͨ¹ýÈöÔÓ¦porttypeµÄJAVAÀàʵÏÖ Provider<Source>½Ó¿Ú,µ±·þÎñÆ÷¶ËÊÕµ½SOAPÏûÏ¢ºó,»áµ ......
±¾ÎÄ×ܽáÁËÔÚPythonÖÐÖ÷ÒªµÄ¼¸ÖÖ´¦ÀíXMLµÄ·½·¨£º
Element Tree
SAX
Dom
ʹÓõÚÈý·½Àà¿âÈçAmara 2.x£¬ libxml2dom µÈ
Element Tree
Element TreeÊÇPython 2.5ÒýÈëµÄʹÓüòµ¥£¬¿ì½ÝµÄ´¦Àí·½·¨£¬ Ò²ÊÇPython±ê×¼Àà¿âÍÆ¼öµÄ´¦ÀíXMLµÄ·½·¨¡£
ËüʹÓÃÊ÷ÐνڵãµÄÐÎʽÀ´¶ÁдXML¡£Element TreeÊÇÇáÁ¿¼¶µÄDOM£¬ ËùÒÔÊ ......
data.xml
<?xml version="1.0" encoding="UTF-8"?>
<Items>
<Item ASIN="0446355453"
Author="Sidney Sheldon"
Manufacturer="Warner Books"
ProductGroup="Book"
Title="Master of the Game"/>
<Item ASIN=" ......