簡體   English   中英

C#將XML划分為多個部分

[英]C# Dividing XML into parts

我試圖將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>

我試圖通過此代碼獲取節點數

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());

但在此之后我無法繼續,這段代碼我用來計算注冊節點的數量然后把它們變成一半,現在我不知道如何進一步分割這個文件,我總共有158718個條目(注冊)文件內部(有時甚至更多),我試圖將所有部分分解,可能是3到4個部分。

試試這個,它不應該將整個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