简体   繁体   English

ReadToFollowing XML大文件

[英]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. 我有一个2GB的xml文件,我正在尝试实现ReadToFollowing方法,但是,这有点时髦。

When I use this I can't get my "if" statements to work. 当我使用它时,我的“ if”语句无法正常工作。 Basically xmlReader.ReadInnerXML() brings back all the nodes under iVehicle. 基本上,xmlReader.ReadInnerXML()带回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). 我假设这是文档中每个iVehicle的所有节点(这可能是我想要的,但是我还需要获取子节点,以便可以填充并返回我的对象​​(针对每个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? 我需要做些什么才能使这项工作?

ReadToFollowing("iVehicle") positions the reader at the start of the next <iVehicle> node So given it succeeds Name is iVehicle ReadToFollowing("iVehicle")将阅读器放置在下一个<iVehicle>节点的开头,因此如果成功,则将其命名为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. 要进入<FamilyName>您需要阅读/移动到它,然后使用.Value或它的一种来获取内容。

Think of XmlReader as moving a pointer through the xml file and then you get the stuff at the pointer. 可以将XmlReader视为在xml文件中移动指针,然后在指针处获得内容。

ReadInnerXml would normally be used to pick a node out and pass it to another XmlReader, so you can break your code up. ReadInnerXml通常用于挑选一个节点并将其传递给另一个XmlReader,因此您可以拆分代码。

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

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