首页 > 解决方案 > LINQ 读取 XML 字符串并放入变量

问题描述

我正在尝试使用 LINQ 读取 XML。以前我使用 XMLDocument 来阅读,但它给出了一个错误,并且 StackOverflow 上的某个人鼓励我使用 LINQ。

下面是我之前用于 XMLDocument 的代码

            string soapmessage = @"<?xml version=""1.0"" encoding=""UTF - 8""?>" + "\n" + response.Content;

            XmlDocument xml = new XmlDocument();
            xml.LoadXml(soapmessage);  //loading soap message as string 

            XmlNamespaceManager manager = new XmlNamespaceManager(xml.NameTable);

            manager.AddNamespace("d", "http://tempuri.org/");
            manager.AddNamespace("bhr", "http://52.187.127.196:5000/api/gsowebservice.asmx");

            XmlNodeList xnList = xml.SelectNodes("//bhr:FourMonthsAhead1Response", manager);
            int nodes = xnList.Count;
            string Status = xnList[0]["FourMonthsAhead1Result"]["PlantForecastIntervals"]["PlantForecastIntervalNode"]["IntervalStartTime"].InnerText;
            Console.WriteLine(Status);
            Console.ReadLine();

我试图<IntervalStartTime>从第一个<PlantForecastIntervalNode>到日期时间变量;

下面附上了正在尝试阅读的 XML:

在此处输入图像描述

下面是一些 XML 代码。我不能在这里粘贴它,因为代码有 2322 行长,所以我将代码缩短为这个。

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <s:Body>
        <FourMonthsAhead1Response xmlns="http://tempuri.org/">
            <FourMonthsAhead1Result xmlns="LSS.solar.webservice">
                <PlantDescription xmlns="http://base.datacontract">*PlantName*</PlantDescription>
                <PlantForecastIntervalsCount xmlns="http://base.datacontract">2976</PlantForecastIntervalsCount>
                <ForecastStartDate xmlns="http://base.datacontract">2021-10-08T13:35:55.912612</ForecastStartDate>
                <ForecastEndDate xmlns="http://base.datacontract">2021-10-08T13:35:55.9126123</ForecastEndDate>
                <PlantForecastIntervals xmlns="http://base.datacontract">
                    <PlantForecastIntervalNode>
                        <IntervalStartTime>2021-10-01T00:00:00</IntervalStartTime>
                        <IntervalEndTime>2021-10-01T00:15:00</IntervalEndTime>
                        <IntervalLength>15</IntervalLength>
                        <ForecastResultParameter>FourMonthsAhead1</ForecastResultParameter>
                        <ForecastValue>0</ForecastValue>
                        <ValueUnit>MW</ValueUnit>
                    </PlantForecastIntervalNode>
                    <PlantForecastIntervalNode>
                        <IntervalStartTime>2021-10-01T00:15:00</IntervalStartTime>
                        <IntervalEndTime>2021-10-01T00:30:00</IntervalEndTime>
                        <IntervalLength>15</IntervalLength>
                        <ForecastResultParameter>FourMonthsAhead1</ForecastResultParameter>
                        <ForecastValue>0</ForecastValue>
                        <ValueUnit>MW</ValueUnit>
                    </PlantForecastIntervalNode>
                </PlantForecastIntervals>
            </FourMonthsAhead1Result>
        </FourMonthsAhead1Response>
    </s:Body>
</s:Envelope>

更新

在探索 StackOverflow 上的其他线程后,我在下面提出了这一行,但收到​​另一个错误System.UriFormatException: 'Invalid URI: The Uri string is too long.'

XDocument xdoc = XDocument.Load(soapmessage);

            var ids = xdoc.Element("FourMonthsAhead1Result")
                 .Elements("PlantForecastIntervals")
                 .Elements("<PlantForecastIntervalNode>")
                 .Select(item => item.Element("IntervalStartTime").Value);
            Console.WriteLine(ids);

标签: c#.netxmlsoapxmldocument

解决方案


试试这个LINQ

var response = File.ReadAllText("XMLFile1.xml");
var xe = XElement.Parse(response);
XNamespace ns = "http://base.datacontract";
var obj = xe.Descendants(ns + "PlantForecastIntervals")
            .Descendants(ns + "PlantForecastIntervalNode")
            .Select(x => x.Element(ns + "IntervalStartTime").Value);
Console.WriteLine(obj);

推荐阅读