简体   繁体   中英

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). If I had to guess I'd say something is wrong with my querying, returning no nodes. Here's a sample of the 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:

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. Use XDocument.Decendants().

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.

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..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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