简体   繁体   中英

how to retrieve unique value from xml

i have written a query to retrieve distinct xml node value using foreach
i am trying to retrive distinct BankName and TemplateModel from XML
can any one just a better way to write the query with ienumerable or something like that
my Xml is as follows

<Bank>
<BankName BankName="DohaBank" TemplateModel="CT1">   
  <ChqBasics>      
  </ChqBasics>
  <XandYPosition>       
  </XandYPosition>
</BankName>
<BankName BankName="DohaBank" TemplateModel="CT2">   
  <ChqBasics>       
  </ChqBasics>
  <XandYPosition>        
  </XandYPosition>    
</BankName>  
<BankName BankName="IBQ" TemplateModel="CT1New">
    <ChqBasics>     
  </ChqBasics>
  <XandYPosition>     
  </XandYPosition>
</BankName> 

my c# code

 public List<string> bankload()
    {
        List<string> bankname=new List<string>();
        XDocument doc = XDocument.Load("newtest.xml");
       var result= (from item in doc.Descendants("BankName")
                select (string)item.Attribute("BankName")).Distinct();
       foreach (var item in result)
       {
           bankname.Add(item.ToString());
       }
        return bankname;
    }

    public static List<string> templateload(string bankname)
    {
        List<string> templatename = new List<string>();
        XDocument doc = XDocument.Load("newtest.xml");
        var result = (from item in doc.Descendants("BankName")
                      where item.Attribute("BankName").Value == bankname
                      select (string)item.Attribute("TemplateModel")).Distinct();
        foreach (var item in result)
        {
            templatename.Add(item.ToString());
        }
        return templatename;
    }

i need to bind the result to a combobox

Why not do this?

public List<string> bankload()
{
    return
    (
        from item in XDocument.Load("newtest.xml").Descendants("BankName")
        select (string)item.Attribute("BankName")
    )
        .Distinct()
        .ToList();
}

public static List<string> templateload(string bankname)
{
    return
    (
        from item in XDocument.Load("newtest.xml").Descendants("BankName")
        where item.Attribute("BankName").Value == bankname
        select (string)item.Attribute("TemplateModel")
    )
        .Distinct()
        .ToList();
}

Do you mean lambdas like this?

public List<string> bankload()
{
    return
        XDocument
            .Load("newtest.xml")
            .Descendants("BankName")
            .Select(item => (string)item.Attribute("BankName"))
            .Distinct()
            .ToList();
}

public static List<string> templateload(string bankname)
{
    return
        XDocument
            .Load("newtest.xml")
            .Descendants("BankName")
            .Where(item => item.Attribute("BankName").Value == bankname)
            .Select(item => (string)item.Attribute("TemplateModel"))
            .Distinct()
            .ToList();
}

Return a list of distinct BankName/TemplateModel combinations in your XML:

var result = doc.Descendants("BankName")
                .Select(bn => new
                              {
                                 BankName = bn.Attribute("BankName").Value,
                                 TemplateModel = bn.Attribute("TemplateModel")
                                                   .Value
                              });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM