C# - Linq - XPathSelectElement 每次都返回相同的结果

本文关键字:结果 返回 Linq XPathSelectElement | 更新日期: 2023-09-27 18:34:51

我在读取一些xml行时遇到了XPathSelectElement的问题......

函数handleEachSession从我的其他函数的mathod childList.ForEach((中调用了两次:

private void handleMonitorResponse(XElement receivedSessions)
 {
 List<XElement> childList = receivedSessions.Elements().ToList();
 childList.ForEach(handleEachSession);

childList 有两个元素(两个 XML 行,我希望它有(然后我可以在调试器中看到handleEachSession((的两个调用都获得了正确的XElement会话。我希望它拥有这两个。

现在有这些XPathSelectElement的。它适用于第一次调用,但不适用于第二次调用。第二次我得到新的 XElement 会话,但 XPathSelectElement 仍然返回第一次调用的旧值。哼!??我无法理解这种行为。

private void handleEachSession(XElement session)
 {
 XElement receivedSessionId = session.XPathSelectElement("//sessionId");
 XElement receivedQuality = session.XPathSelectElement("//quality");
 XElement receivedContentStatus = session.XPathSelectElement("//contentStatus");
…

谁能帮我?

这些 XML 是:

receivedSessions = 
<sessions type="array">
  <item type="object">
    <sessionId type="string">8c86716a87</sessionId>
    <quality type="string">mid</quality>
    <addressUri type="string">http://192.168.6.234/media/SorrowPinkFloyd_.mp4</addressUri>
    <currentTime type="number">1.5542887</currentTime>
    <contentStatus type="string">paused</contentStatus>
  </item>
  <item type="object">
    <sessionId type="string">c795067c4e</sessionId>
    <quality type="string">mid</quality>
    <addressUri type="string">http://192.168.6.234/media/trailer.mp4</addressUri>
    <currentTime type="number">3.3492105</currentTime>
    <contentStatus type="string">playing</contentStatus>
  </item>
</sessions>
session = (first call)
<item type="object">
  <sessionId type="string">8c86716a87</sessionId>
  <quality type="string">mid</quality>
  <addressUri type="string">http://192.168.6.234/media/SorrowPinkFloyd_.mp4</addressUri>
  <currentTime type="number">1.5542887</currentTime>
  <contentStatus type="string">paused</contentStatus>
</item>
session = (second call)
<item type="object">
  <sessionId type="string">c795067c4e</sessionId>
  <quality type="string">mid</quality>
  <addressUri type="string">http://192.168.6.234/media/trailer.mp4</addressUri>
  <currentTime type="number">3.3492105</currentTime>
  <contentStatus type="string">playing</contentStatus>
</item>

C# - Linq - XPathSelectElement 每次都返回相同的结果

根据 xpath-syntax,双斜杠 (//( 表示: 从当前节点中选择文档中与所选内容匹配的节点,无论它们位于何处。因此,考虑到这一点,您可能需要将handleEachSession函数更改为

private void handleEachSession(XElement session)
{
    XElement receivedSessionId = session.XPathSelectElement("sessionId");
    XElement receivedQuality = session.XPathSelectElement("quality");
    XElement receivedContentStatus = session.XPathSelectElement("contentStatus");
}