0

我正在尝试使用 xdoc 和 LINQ 在以下 xml 文件中获取样式列表。

<?xml version="1.0" encoding="UTF-8"?>
<kml>
  <Document>
    <Style id="style62">
      <IconStyle>
        <Icon>
          <href>http://maps.gstatic.com/mapfiles/ms2/micons/yellow-dot.png</href>
        </Icon>
      </IconStyle>
    </Style>
  </Document>
</kml>

为了在同一个 LINQ 选择中获取 ID="style62" 以及 href 中的值,我无法正确使用语法,有人可以帮忙吗?

var styles = xdoc.Descendants(ns + "Style")
                .Select(s => new
                {
                    //HELP!?!
                    //E.G
                    //
                    //id = s.something  (style62)
                    //href = s.something (url)
                }).ToList();
4

4 回答 4

4

如果您正在谈论像这里https://developers.google.com/kml/documentation/KML_Samples.kml这样的 kml 文件, 那么下面的代码应该可以工作。这里的问题是每个“样式”都不包含“href”标签。

var xDoc = XDocument.Parse(xml);
XNamespace ns = "http://www.opengis.net/kml/2.2";
var items = xDoc.Descendants(ns + "Style")
                .Select(d => 
                {
                   var h = d.Descendants(ns + "href").FirstOrDefault();
                   return new
                   {
                       Id = d.Attribute("id").Value,
                       Href = h == null ? null : h.Value
                   };
                })
                .ToList();

使用简单的扩展方法,可以简化查询

XNamespace ns = "http://www.opengis.net/kml/2.2";
var items = xDoc.Descendants(ns + "Style")
                .Select(d => new
                {
                   Id = d.Attribute("id").Value,
                   HRef = d.Descendants(ns + "href").FirstOrDefault()
                                                    .IfNotNull(h=>h.Value)
                })
                .ToList();



public static class S_O_Extensions
{
    public static S IfNotNull<T, S>(this T obj,Func<T,S> selector)
    {
        if (obj == null) return default(S);
        return selector(obj);
    }
 }
于 2012-10-21T16:43:59.100 回答
0

通过 LinqPad 运行:

XDocument doc = XDocument.Parse("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<kml>" +
  "<Document>" +
    "<Style id=\"style62\">" +
      "<IconStyle>" +
        "<Icon>" +
          "<href>http://maps.gstatic.com/mapfiles/ms2/micons/yellow-dot.png</href>" +
        "</Icon>" +
      "</IconStyle>" +
    "</Style>" +
  "</Document>" +
"</kml>");

var styles = from document in doc.Root.Elements("Document")
            from style in document.Elements("Style")
            where style.Attribute("id").Value == "style62"
            select new 
            {
                StyleElement = style,
                Href = style.Element("IconStyle").Element("Icon").Element("href").Value
            };


styles.Dump();
于 2012-10-21T16:45:07.370 回答
0

你可以像使用 linq

var items = doc.Descendants("field")
           .Where(node => (string)node.Attribute("name") == "Name")
           .Select(node => node.Value.ToString())
           .ToList();
于 2012-10-21T16:49:20.833 回答
0

像这样的东西应该工作:

xdoc.Descendants(ns + "Style")
    .Select(s => new
                 {
                     id = s.Attribute("id").Value,
                     href = s.Element("IconStyle")
                             .Element("Icon")
                             .Element("href")
                             .Value
                 });
于 2012-10-21T16:41:47.917 回答