简体   繁体   English

Linq to XML没有值

[英]Linq to XML giving no values

I just can't seem to figure out whats wrong with this code... Any idea's what I'm doing wrong. 我只是似乎无法弄清楚这段代码有什么问题...任何想法都是我做错了什么。 The select returns one item, with all null attributes, ie the foreach loop gets entered once and the WMServer Server.[pick attribute] is set to null. 该选择返回一项具有所有空属性的项目,即,一次进入foreach循环,并且WMServer服务器。[pick属性]设置为null。

var xdoc = XDocument.Load(@"pathtoxmlfile.xml");
var wms = from e2 in xdoc.Elements("GISImportConfig").Elements("BaseMapLayers")
          select new
          {
              Url = (string)e2.Attribute("url"),
              Enabled = (string)e2.Attribute("enabled"),
              UserName = (string)e2.Attribute("username"),
              Pasword = (string)e2.Attribute("password"),
              Layers = e2.Elements("WMLayer")
          };

foreach (var Config in wms)
{
    WMServer server = new WMServer();
    server.ServerURL = Config.Url;
    server.Enabled = Convert.ToBoolean(Config.Enabled);
    server.UserName = Config.UserName;
    server.Password = Config.Pasword;

    foreach (var layers in Config.Layers)
    {
        WMLayer layer = new WMLayer();
        layer.Group = (string)layers.Attribute("group");
        layer.Enabled = Convert.ToBoolean(layers.Attribute("enabled"));
        layer.Name = (string)layers.Attribute("name");
    }
}

xml: XML:

<?xml version="1.0" encoding="utf-8" ?>
<GISImportConfig OracleServer="*" OracleInstance="*"   OracleSchema="*">
  <BaseMapLayers>
    <WMServer  url="https://example" enabled="true" username="someuser" password="somepass">
      <WMLayer name="0" enabled ="true" group="test"></WMLayer>
      <WMLayer name="1" enabled ="true" group="test"></WMLayer>
    </WMServer>
    <WMServer url="server2" enabled="false" username="" password="">
      <WMLayer name="test2" enabled ="true" group="test"></WMLayer>
    </WMServer>
  </BaseMapLayers>
</GISImportConfig>

You get only one result, because you have only one <BaseMapLayers> element, and that's the one you're looking for with your query. 您只会得到一个结果,因为只有一个<BaseMapLayers>元素,这就是您要在查询中查找的元素。 Add .Elements("WMServer") method call and it should work: 添加.Elements("WMServer")方法调用,它应该可以工作:

from e2 in xdoc.Elements("GISImportConfig").Elements("BaseMapLayers").Elements("WMServer")
select new
{
    Url = (string)e2.Attribute("url"),
    Enabled = (string)e2.Attribute("enabled"),
    UserName = (string)e2.Attribute("username"),
    Pasword = (string)e2.Attribute("password"),
    Layers = e2.Elements("WMLayer")
};

Returns 2 elements with non-null attribute values. 返回2个具有非null属性值的元素。

It looks like you're trying to get the WMServer elements and you're currently selecting the BaseMapLayers element (of which only one exists). 看起来您正在尝试获取WMServer元素,并且当前正在选择BaseMapLayers元素(其中只有一个)。

You could get the WMServer elements by doing this: 您可以通过执行以下操作获取WMServer元素:

from e2 in xdoc.Descendants("WMServer")
select new
{
      Url = (string)e2.Attribute("url"),
      Enabled = (string)e2.Attribute("enabled"),
      UserName = (string)e2.Attribute("username"),
      Pasword = (string)e2.Attribute("password"),
      Layers = e2.Elements("WMLayer")
};

which will grab you all the WMServer descendent elements. 它将抓住您所有的WMServer后代元素。

I think MarcinJuraszek has a better answer (appending the relevant element), as there's less scope to run into problems if you find a misplaced WMServer element, or only want those at that specific location in the xml. 我认为MarcinJuraszek(在相关元素后面)有更好的答案,因为如果发现WMServer元素WMServer错误,或者只希望xml中的特定位置出现问题,则遇到问题的范围会更小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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