简体   繁体   English

C#将XML划分为多个部分

[英]C# Dividing XML into parts

I am trying to divide an XML file into parts I have an XML file like this 我试图将XML文件划分为我有这样的XML文件的部分

<?xml version="1.0" encoding="utf-8"?>
<RegistrationOpenData xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://example.gov">
<Description>Registration data is collected by ABC XYZ</Description>
<InformationURL>http://www.example.com/html/hpd/property-reg-unit.shtml</InformationURL>
<SourceAgency>ABC Department of Housing</SourceAgency>
<SourceSystem>PREMISYS</SourceSystem>
<StartDate>2016-02-29T00:03:06.642772-05:00</StartDate>
<EndDate i:nil="true" />
<Registrations>
<Registration xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<RegistrationID>1</RegistrationID>
<BuildingID>1A</BuildingID>
<element1>E11</element1>
<element2>E21</element2>
<element3>E31</element3>
<element4>E41</element4>
</Registration>
<Registration xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<RegistrationID>2</RegistrationID>
<BuildingID>2A</BuildingID>
<element1>E21</element1>
<element2>E22</element2>
<element3>E32</element3>
<element4>E42</element4>
</Registration>
</Registrations>
</RegistrationOpenData>

And I am trying to fetch the number of nodes trough this code 我试图通过此代码获取节点数

XmlDocument doc = null;
doc = new XmlDocument();
doc.Load(@"D:\Registrations20160229.xml");
XmlNodeReader nodeReader = new XmlNodeReader(doc);
XmlElement root = doc.DocumentElement;
XmlNodeList elemList = root.GetElementsByTagName("Registration");
int totalnode = elemList.Count;
int nodehalf = totalnode / 2;
MessageBox.Show(nodehalf.ToString());

But after this I am unable to proceed, This code I have used to calculate number of Registration Nodes and then made them into half, now I don't know how to proceed further to split this file, I have total of 158718 entries (Registration Nodes) inside the file (sometimes even more) and I am trying to break all into parts, maybe 3 to 4 parts. 但在此之后我无法继续,这段代码我用来计算注册节点的数量然后把它们变成一半,现在我不知道如何进一步分割这个文件,我总共有158718个条目(注册)文件内部(有时甚至更多),我试图将所有部分分解,可能是3到4个部分。

Try this , it should not load whole xml to memory 试试这个,它不应该将整个xml加载到内存中

        using(XmlReader reader = XmlReader.Create(new FileStream(@"D:\Registrations20160229.xml" , FileMode.Open))){
                        while (reader.Read())
        {
            if(reader.NodeType == XmlNodeType.Element && reader.Name == "Registration")
                counter++;
        }
        Console.WriteLine(counter);
        }

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

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