C#XML复杂节点列表-将每个复杂值存储到新对象中

C#XML复杂节点列表-将每个复杂值存储到新对象中,c#,asp.net,.net,xml,xmlnode,C#,Asp.net,.net,Xml,Xmlnode,我有这样的.XML文件(我通过C#中JSON的XML序列化得到了这个文件,这正是我想要的): -->这是历史贸易的第一个“最后一个”价值** {operating_mic: WWW mic: WWW isin: NO1234567 feed: 18888 ticker: ddd currency: CHF last: 133.1} -->这是历史交易的第二个“最后一个”价值,所有其他财产都是一样的 我试着用这个,但没用: XDoc.LoadXml(histPr

我有这样的.XML文件(我通过C#中JSON的XML序列化得到了这个文件,这正是我想要的):

-->这是历史贸易的第一个“最后一个”价值**

  {operating_mic: WWW
  mic: WWW
  isin: NO1234567
  feed: 18888
  ticker: ddd
  currency: CHF
  last: 133.1}
-->这是历史交易的第二个“最后一个”价值,所有其他财产都是一样的

我试着用这个,但没用:

   XDoc.LoadXml(histPricesFormatted);
                XmlNodeList PriceNodes = XDoc.SelectNodes("//historical_data/HistroicalData");
                SecurityPrice price = new SecurityPrice(xmlDoc);

                if (PriceNodes.Count == 0)
                {
                    return;
                }
                else
                {

                    foreach (XmlNode xn in PriceNodes)
                    {
                        if (xn["historical_trades"]!= null)
                        {

                            XmlNodeList histTradeNode = xn["historical_trades"].SelectNodes("//historical_trades/HistoricalTrades");
                            foreach (XmlNode trade in histTradeNode)
                            {
                                if (xn["ticker"] != null) price.ID = xn["ticker"].InnerText;
                                if (xn["currency"] != null) price.Currency = xn["currency"].InnerText;
                                if (xn["isin"] != null) price.ISIN = xn["isin"].InnerText;
                                if (trade["last"] != null) price.Price = double.Parse(xn["last"].InnerText);
                                if (xn["error_description"] != null) price.Notes = xn["error_description"].InnerText;
                            }
                        }
                    }
有人知道我错在哪里吗?

使用Xml Linq:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XNamespace ns = doc.Root.GetDefaultNamespace();

            var historicalData = doc.Descendants(ns + "HistroicalData").Select(x => new
            {
                operating_mic = (string)x.Element(ns + "operating_mic"),
                mic = (string)x.Element(ns + "mic"),
                isin = (string)x.Element(ns + "isin"),
                feed = (string)x.Element(ns + "feed"),
                ticker = (string)x.Element(ns + "ticker"),
                currency = (string)x.Element(ns + "currency"),
                trades = x.Descendants(ns + "last").Select(y => (decimal)y).ToList()
            }).Select(x => x.trades.Select(trade => new {
                operatring_mic = x.operating_mic,
                mic = x.mic,
                isin = x.isin,
                feed = x.feed,
                ticker = x.ticker,
                currency = x.currency,
                trade = trade
            })).SelectMany(x => x).ToList();
        }
    }
}

请解释一下你所说的“无效”是什么意思。有例外吗?如果是,在哪一行?定义行为,这样会更容易提供帮助。
  {operating_mic: WWW
  mic: WWW
  isin: NO1234567
  feed: 18888
  ticker: ddd
  currency: CHF
  last: 133.1}
   XDoc.LoadXml(histPricesFormatted);
                XmlNodeList PriceNodes = XDoc.SelectNodes("//historical_data/HistroicalData");
                SecurityPrice price = new SecurityPrice(xmlDoc);

                if (PriceNodes.Count == 0)
                {
                    return;
                }
                else
                {

                    foreach (XmlNode xn in PriceNodes)
                    {
                        if (xn["historical_trades"]!= null)
                        {

                            XmlNodeList histTradeNode = xn["historical_trades"].SelectNodes("//historical_trades/HistoricalTrades");
                            foreach (XmlNode trade in histTradeNode)
                            {
                                if (xn["ticker"] != null) price.ID = xn["ticker"].InnerText;
                                if (xn["currency"] != null) price.Currency = xn["currency"].InnerText;
                                if (xn["isin"] != null) price.ISIN = xn["isin"].InnerText;
                                if (trade["last"] != null) price.Price = double.Parse(xn["last"].InnerText);
                                if (xn["error_description"] != null) price.Notes = xn["error_description"].InnerText;
                            }
                        }
                    }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XNamespace ns = doc.Root.GetDefaultNamespace();

            var historicalData = doc.Descendants(ns + "HistroicalData").Select(x => new
            {
                operating_mic = (string)x.Element(ns + "operating_mic"),
                mic = (string)x.Element(ns + "mic"),
                isin = (string)x.Element(ns + "isin"),
                feed = (string)x.Element(ns + "feed"),
                ticker = (string)x.Element(ns + "ticker"),
                currency = (string)x.Element(ns + "currency"),
                trades = x.Descendants(ns + "last").Select(y => (decimal)y).ToList()
            }).Select(x => x.trades.Select(trade => new {
                operatring_mic = x.operating_mic,
                mic = x.mic,
                isin = x.isin,
                feed = x.feed,
                ticker = x.ticker,
                currency = x.currency,
                trade = trade
            })).SelectMany(x => x).ToList();
        }
    }
}