简体   繁体   English

C#的XML解析帮助

[英]XML parsing help for C#

I'm getting my feet wet with linq to xml, and I have the data in memory but the following code is running without error, but without adding my objects to the data point list (the end of the procedure below). 我正在用linq to xml弄湿了我的数据,并且在内存中存储了数据,但是以下代码正在运行,没有错误,但是没有将我的对象添加到数据点列表中(以下过程的结尾)。 If I had to guess I'd say something is wrong with my querying, returning no nodes. 如果我不得不猜测我的查询有问题,不返回任何节点。 Here's a sample of the xml: 这是xml的示例:

<results>
            <quote date="2012-02-07">
              <Date>2012-02-07</Date>
              <Open>44.76</Open>
              <High>44.88</High>
              <Low>44.22</Low>
              <Close>44.60</Close>
              <Volume>2547400</Volume>
              <Adj_Close>44.60</Adj_Close>
            </quote>

And here's my linq and relevant code: 这是我的linq和相关代码:

List<IDataPoint> dataPointList = new List<IDataPoint>();
                XDocument doc = XDocument.Load(AddressString);

var makeInfo =
                    from s in doc.Elements("quote")
                    where s.Element("Date") != null && s.Element("Open") != null
                        && s.Element("High") != null && s.Element("Low") != null
                        && s.Element("Close") != null && s.Element("Volume") != null
                        && !s.Element("Open").Value.Equals("") && !s.Element("High").Value.Equals("")
                        && !s.Element("Low").Value.Equals("") && !s.Element("Close").Value.Equals("")
                    select new DailyPricingVolDP(symbol, (DateTime)s.Element("Date"),
                        (double)s.Element("Open"),
                        (double)s.Element("High"),
                        (double)s.Element("Low"),
                        (double)s.Element("Close"),
                        (long)s.Element("Volume"));

                foreach (var item in makeInfo)
                {
                    dataPointList.Add(item);
                }

I'm pretty sure XDocument.Elements() only returns direct children, and based on your XML doc.Elements("quote") will not match anything. 我很确定XDocument.Elements()仅返回直接子级,并且基于您的XML doc.Elements(“ quote”)将不匹配任何内容。 Use XDocument.Decendants(). 使用XDocument.Decendants()。

IE doc.Descendants("quote") IE doc.Descendants(“ quote”)

Try 尝试

var makeInfo = 
                    from s in doc.Element("result").Elements("quote") 
                    where s.Element("Date") != null && s.Element("Open") != null 
                        && s.Element("High") != null && s.Element("Low") != null 
                        && s.Element("Close") != null && s.Element("Volume") != null 
                        && !s.Element("Open").Value.Equals("") && !s.Element("High").Value.Equals("") 
                        && !s.Element("Low").Value.Equals("") && !s.Element("Close").Value.Equals("") 
                    select new DailyPricingVolDP(symbol, (DateTime)s.Element("Date"), 
                        (double)s.Element("Open"), 
                        (double)s.Element("High"), 
                        (double)s.Element("Low"), 
                        (double)s.Element("Close"), 
                        (long)s.Element("Volume")); 

I'd also recommend writing, for example, s.Element("Open").Value != string.Empty instead of !s.Element("Open").Value.Equals("") , but that's a stylistic thing. 我也建议编写例如s.Element("Open").Value != string.Empty而不是!s.Element("Open").Value.Equals("") ,但这是一种风格。

I got it on my own but thanks for the quick help... 我自己买的,但感谢您的快速帮助...

doc.Elements("quote")

needs to be: 需要是:

doc.Descendants("quote")

and it works as expected. 并且它按预期工作。 Thanks again.. 再次感谢..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM