webdad3
webdad3

Reputation: 9080

ReadToFollowing XML Large File

I have a 2GB xml file and I'm trying to implement the ReadToFollowing method however, it is a little funky.

When I use this I can't get my "if" statements to work. Basically xmlReader.ReadInnerXML() brings back all the nodes under iVehicle. I'm assuming it is all the nodes for each iVehicle in the document (which is probably what I want, but I also need the get the child nodes so I can populate and return my object (for each iVehicle).

        using (FileStream stream = new FileStream(uri, FileMode.Open, FileAccess.Read))
        {
            XmlTextReader xmlReader = new XmlTextReader(stream);

            while (xmlReader.ReadToFollowing("iVehicle"))
            {
                if(xmlReader.NodeType == XmlNodeType.Element && xmlReader.Name.Equals("FamilyName"))
                {
                    fundFamilyId = xmlReader.ReadInnerXml();
                }
                if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.Name.Equals("InvestmentVehicle"))
                {
                    secId = xmlReader.GetAttribute("_Id").ToString();
                }

                if (el.NodeType == XmlNodeType.Element && el.Name == "TradingSymbol")
                {
                    ticker = xmlReader.ReadInnerXml();
                }

                yield return new parsedXML
                {
                    Id = secId,
                    FamilyName = familyName,
                    TickerId = ticker,
                };

            } 
        }

What do I need to do to make this work?

Upvotes: 0

Views: 2588

Answers (1)

Tony Hopkinson
Tony Hopkinson

Reputation: 20320

ReadToFollowing("iVehicle") positions the reader at the start of the next <iVehicle> node So given it succeeds Name is iVehicle

To get to <FamilyName> you need to Read / Move to it and then use .Value or one it's flavours to get the content.

Think of XmlReader as moving a pointer through the xml file and then you get the stuff at the pointer.

ReadInnerXml would normally be used to pick a node out and pass it to another XmlReader, so you can break your code up.

Upvotes: 1

Related Questions