试图从XDocument中获取单个值时,似乎什么都不起作用

本文关键字:什么 不起作用 单个值 XDocument 获取 | 更新日期: 2023-09-27 18:29:24

我只是想从MessageInfo中获取值。。Sender这里是xml的摘录。我只想要"Senders"值。我使用XDocument尝试了许多不同的方法,并希望使用LinqQuery我试过了,

var query1 = doc.Descendants("MessageInfo").Select(s => new MessageInfo
{
     SYSGENID = s.Element("SysGenID").Value,
     TIME_STAMP = s.Element("TimeStamp").Value,
     SENDER = s.Element("Sender").Value,
     RECEIVER = s.Element("Receiver").Value,
     ENTITY_CODE = s.Element("EntityCode").Value
}).FirstOrDefault();

query1返回null。以下是xml 的示例

我也试过

XDocument doc = XDocument.Load(filePath);
               var messageInfo = doc.Root.Elements("MessageInfo");
               var res = from m in messageInfo
                         select new
                         {
                             msgInfo = m.Element("MessageInfo").Value
                         };
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"   
 xmlns:xsi="http://www.w3.org/2001/XMLSchema instance"  
                  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
      <PutSchedule xmlns="http://www.nwpp.org/eide">
        <MessageInfo>
          <SysGenID>4431</SysGenID>
          <TimeStamp>2014-08-12T10:34:28.068000</TimeStamp>
          <Sender>611</Sender>
          <Receiver>WECC</Receiver>
          <EntityCode>611</EntityCode>
        </MessageInfo>
        <Schedules>
          <Schedule>
            <ScheduleDescription>
              <StartTime>2014-08-12T00:00:00</StartTime>
              <EndTime>2014-08-15T00:00:00</EndTime>
              <AccountCode>259S.NRGREEN_G1.BaseMW</AccountCode>
            </ScheduleDescription>
            <Quantities>

试图从XDocument中获取单个值时,似乎什么都不起作用

您有一个必须使用的命名空间。

XNamespace ns = "http://www.nwpp.org/eide";

var query1 = doc.Descendants(ns +"MessageInfo").Select(s => new MessageInfo
                             {
                                 SYSGENID = s.Element(ns +"SysGenID").Value,
                                 TIME_STAMP = s.Element(ns +"TimeStamp").Value,
                                 SENDER = s.Element(ns +"Sender").Value,
                                 RECEIVER = s.Element(ns +"Receiver").Value,
                                ENTITY_CODE = s.Element(ns +"EntityCode").Value
                             }).FirstOrDefault();