代码之家  ›  专栏  ›  技术社区  ›  Perpetualcoder

linq到xml文档遍历

  •  2
  • Perpetualcoder  · 技术社区  · 15 年前

    我有一个这样的XML文档:

    <?xml version="1.0" encoding="utf-8" ?>
    <demographics>
        <country id="1" value="USA">
            <state id ="1" value="California">
                <city>Long Beach</city>
                <city>Los Angeles</city>
                <city>San Diego</city>
            </state>
            <state id ="2" value="Arizona">
                <city>Tucson</city>
                <city>Phoenix</city>
                <city>Tempe</city>
            </state>
        </country>
        <country id="2" value="Mexico">
            <state id ="1" value="Baja California">
                <city>Tijuana</city>
                <city>Rosarito</city>            
            </state>
        </country>
    </demographics> 
    

    如何设置LINQ查询以执行以下操作: 1。得到所有国家 2。获取一个国家的所有州 三.把所有的城市都集中在一个偏僻的国家里?

    我试了一下,我有点困惑什么时候使用元素[“nodename”]和子元素等等,我知道我不是最聪明的xml人。对于简单的遍历,XML文件的格式是否甚至正确?

    3 回复  |  直到 15 年前
        1
  •  4
  •   Mark Byers    15 年前

    从文件加载文档:

    XDocument document = XDocument.Load("input.xml");
    

    要获取所有国家的名称:

    IEnumerable<string> countries = document
        .Descendants("country")
        .Select(element => element.Attribute("value").Value);
    

    为了得到美国境内的所有州:

    IEnumerable<string> states = document
        .Descendants("country")
        .Where(element => element.Attribute("value").Value == "USA")
        .Elements("state")
        .Select(element => element.Attribute("value").Value);
    

    要了解美国/加州的所有城市:

    IEnumerable<string> cities = document
        .Descendants("country")
        .Where(element => element.Attribute("value").Value == "USA")
        .Elements("state")
        .Where(element => element.Attribute("value").Value == "California")
        .Elements("city")
        .Select(element => element.Value);
    

    您可能还需要查看xpath查询(您需要 using System.XML.XPath ):

    IEnumerable<string> cities = document
        .XPathSelectElements("/demographics/country[@value='USA']/state[@value='California']/city")
        .Select(element => element.Value);
    
        2
  •  1
  •   SLaks    15 年前

    这样地:

    var countries = document.Root.Elements("country");
    var states    = country.Elements("state");
    var cities    = state.Elements("city");
    
        3
  •  1
  •   Domenic    15 年前
    var doc = XDocument.Load("myxml.xml");
    
    
    var countries = doc.Descendants("country")
                       .Attributes("value")
                       .Select(a => a.Value);
    
    var states    = doc.Descendants("country")
                       .Single(country => country.Attribute("value").Value == "USA")
                       .Elements("state")
                       .Attributes("value")
                       .Select(a => a.Value);
    
    var cities    = doc.Descendants("state")
                       .Single(state => state.Attribute("value").Value == "California")
                       .Elements("city")
                       .Select(e => e.Value);
    

    结果会有 countries , states cities 作为类型 IEnumerable<string> .

    同样值得注意的是,执行(即解析)将被延迟,直到您实际枚举那些 IEnumerable<字符串> 变量。这有时会导致意外的性能问题。例如,如果您计划无论如何显示所有数据,并将其数据绑定到某个ui控件,则用户界面可能会变得迟缓,因为它意识到它毕竟需要解析这些数据。(它甚至可能会阻塞ui线程,而不是您的工作线程?不确定)要修复此问题,请添加 .ToList() 到最后,得到不延迟 List<string> 而是S。