开发者

LINQ to XML. Enumeration yielded no results?

开发者 https://www.devze.com 2023-01-22 23:19 出处:网络
I cannot figure out why Enumeration does not yield any results Dim Configuration = From Setup In XElement.Load(\"C:\\xml\\setup.xml\").Elements(\"settings\")

I cannot figure out why Enumeration does not yield any results

Dim Configuration = From Setup In XElement.Load("C:\xml\setup.xml").Elements("settings")
                    Select Setup
For Each Setting As String In Configuration
    'Do something
Next

And here is the Xml file located at C:/xml/setup.xml

<?xml version="1.0" encoding="utf-8"?>
<settings>
  <columns>10</columns>
  <rows>3</rows>
  <thumbWidth>1024</thumbWidth>
  <thumbHeight>开发者_JS百科768</thumbHeight>
  <thumbPadding>40</thumbPadding>
  <videoWidth>181</videoWidth>
  <videoHeight>136</videoHeight>
  <CaptionPosition>top</CaptionPosition>
  <Autoplay>no</Autoplay>
  <Delay>6</Delay>
  <Target>_blank</Target>
</settings>

What am i doing wrong here?


You've called XElement.Load, which will return the settings element itself. In other words, you're currently looking for elements called settings inside the settings element.

Options:

  • Use XDocument.Load instead
  • Just use the loaded XElement itself, instead of calling Elements - with the settings element being the root element, there can only be one of them anyway.
  • Make your XML document have a root element, e.g.

    <root>
      <settings>
      ...
      </settings>
      <settings>
      ...
      </settings>
    </root>
    
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号