2016-06-14 79 views
1

我試圖劃分一個XML文件到部分 我有這樣C#分水嶺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> 

一個XML文件,我試圖獲取節點的數量低谷這段代碼

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部分。

+0

你能告訴我們實際的症狀是什麼,你看到的?不需要將文件分成3至4個部分。當你說「因規模而失敗」時,你可以詳細瞭解一下這方面的情況嗎?你收到了什麼錯誤信息? –

+0

我試圖將它加載到一個數組中,然後運行「for循環」以獲得30,000個條目的一部分,但它無法一次加載完整文件並顯示內存不足異常 – Hanumendra

+0

請顯示無法使用的代碼。您向我們展示了一段代碼,然後告訴我們一個_different_代碼塊不起作用。 –

回答

0

試試這個,它不應該整個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); 
     } 
+0

計算節點數量與上面的代碼正常工作,我怎樣才能將xml分成幾部分? – Hanumendra

+0

所以,如果你想要得到註冊節點裏面,如果把這個:XElement el =(XElement)XNode.ReadFrom(reader) – blackcat

+0

〜Hanumendra這裏的問題是,你已經決定將XML分成多個部分,使它更小是方式轉發給你。它可能不是。請更新您的問題以包含您所寫的代碼,錯誤信息*以及您的最終目標*。看起來我們可能有XY問題(請參閱http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)。 –