LINQ 到 XML 错误“不是“String”的成员”

发布于 2024-08-26 05:40:22 字数 1331 浏览 4 评论 0原文

以下代码返回 For Each 循环的错误。我有类似的代码,但不返回错误。

“DisplayTitle”不是“Sting”的成员

Dim evXML As XDocument = XDocument.Load(Server.MapPath("~/App_Data/event.xml"))

Dim sbEventDetail As New StringBuilder()
Dim summary = _
From sum In evXML.<root>.Elements() _
Select sum...<DisplayTitle>.Value

For Each item In summary
    sbEventDetail.Append("<h4>" & item.DisplayTitle & "</h4>")
Next

XML:

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <StartTime>2010-03-05T16:00:00</StartTime>
  <EndTime>2010-03-06T02:00:00</EndTime>
  <Duration>10:00:00</Duration>
  <DisplayTitle>MARCH MADNESS</DisplayTitle>
  <Location>565 Main St</Location>
  <IsAllDay>False</IsAllDay>
  <Recurrence>
    <OriginatingTimeZone>Eastern Standard Time</OriginatingTimeZone>
    <RecurrenceType>0</RecurrenceType>
    <RecurrenceEndDate>9999-12-31T23:59:59</RecurrenceEndDate>
  </Recurrence>
  <IsVariance>False</IsVariance>
  <IsCancelled>False</IsCancelled>
  <OriginalStart>0001-01-01T00:00:00</OriginalStart>
</root>

The following code returns the error from the For Each loop. I have similar code that does not return the error.

'DisplayTitle' is not a member of 'Sting'

Dim evXML As XDocument = XDocument.Load(Server.MapPath("~/App_Data/event.xml"))

Dim sbEventDetail As New StringBuilder()
Dim summary = _
From sum In evXML.<root>.Elements() _
Select sum...<DisplayTitle>.Value

For Each item In summary
    sbEventDetail.Append("<h4>" & item.DisplayTitle & "</h4>")
Next

The XML:

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <StartTime>2010-03-05T16:00:00</StartTime>
  <EndTime>2010-03-06T02:00:00</EndTime>
  <Duration>10:00:00</Duration>
  <DisplayTitle>MARCH MADNESS</DisplayTitle>
  <Location>565 Main St</Location>
  <IsAllDay>False</IsAllDay>
  <Recurrence>
    <OriginatingTimeZone>Eastern Standard Time</OriginatingTimeZone>
    <RecurrenceType>0</RecurrenceType>
    <RecurrenceEndDate>9999-12-31T23:59:59</RecurrenceEndDate>
  </Recurrence>
  <IsVariance>False</IsVariance>
  <IsCancelled>False</IsCancelled>
  <OriginalStart>0001-01-01T00:00:00</OriginalStart>
</root>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那请放手 2024-09-02 05:40:22

查看您选择的内容:

Select sum...<DisplayTitle>.Value

Value 属性返回一个字符串 - 因此 summary 变量的类型为 IEnumerable(Of String)

您可能只需要:

For Each item In summary
    sbEventDetail.Append("<h4>" & item & "</h4>")
Next

...假设您不需要任何 HTML 转义,请注意。

Look at what you're selecting:

Select sum...<DisplayTitle>.Value

The Value property returns a string - so the type of the summary variable is IEnumerable(Of String).

You probably just need:

For Each item In summary
    sbEventDetail.Append("<h4>" & item & "</h4>")
Next

... assuming you don't need any HTML-escaping, mind you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文