2012-02-28 34 views
0

我有一個巨大的XML文件,其中的一些節點是可選的。我無法從中得到投標人信息,任何人都可以請幫忙。第2條XML文件給出如下從巨大的xml文件中的可選XML節點中讀取值

<reports> 
<report> 

<xml-report> 
<summary> 
<dr-nbr>2012004</dr-nbr> 
</summary> 
<data> 
<proj-title> 
<title-code> 
<title ci = "N">District 321</title> 
</title-code> 
</proj-title> 
<p-location> 
<project-location> 
<title-code> 
<p-county-name ci = "N">BOOLE</p-county-name> 
<p-fips-county>MT101</p-fips-county> 
<p-city-name ci = "N">MTELBY</p-city-name> 
<p-state-id ci = "N">MT</p-state-id> 
<p-zip-code ci = "N">69474</p-zip-code> 
<p-zip-code5 ci = "N">69474</p-zip-code5> 
<p-country-id ci = "N">USA</p-country-id> 
</title-code> 
</project-location> 
<pct-project-county> 
<title-code> 
<p-county-name ci = "N">TOOLE</p-county-name> 
<p-fips-county>MT101</p-fips-county> 
<p-state-id>MT</p-state-id> 
<p-country-id>USA</p-country-id> 
</title-code> 
</pct-project-county> 
</p-location> 
<status> 
<title-code> 
<status-proj-dlvry-sys ci = "N">Design-Bid-Build</status-proj-dlvry-sys> 
</title-code> 
</status> 
</data> 
</xml-report> 
</report> 

<report> 
<xml-report> 
<summary> 
<dr-nbr>2011005</dr-nbr> 
</summary> 
<data> 
<proj-title> 
<title-code> 
<title ci = "N">Plane Pitch</title> 
</title-code> 
</proj-title> 
<p-location> 
<project-location> 
<title-code> 
<p-county-name ci = "A">SUMMIT</p-county-name> 
<p-fips-county>MI153</p-fips-county> 
<p-city-name ci = "A">AVON</p-city-name> 
<p-state-id ci = "A">MI</p-state-id> 
<p-zip-code ci = "C">44308</p-zip-code> 
<p-zip-code5 ci = "C">44308</p-zip-code5> 
<p-country-id ci = "A">USA</p-country-id> 
</title-code> 
</project-location> 
<pct-project-county> 
<title-code> 
<p-county-name ci = "A">SUMMIT</p-county-name> 
<p-fips-county>OH153</p-fips-county> 
<p-state-id>OH</p-state-id> 
<p-country-id>USA</p-country-id> 
</title-code> 
</pct-project-county> 
</p-location> 

<project-bidder-information> 
<title-code> 
<bid-header> 
<bid-header-desc ci = "N">Low Bidders</bid-header-desc> 
<bid-title> 
<bid-details> 
<contact-information> 
<firm-name>Many Stocks</firm-name> 
<contact-name>Many Moree</contact-name> 
</contact-information> 
</bid-details> 
<bid-details> 
<contact-information> 
<firm-name>Who Constrcution</firm-name> 
</contact-information> 
</bid-details> 
</bid-title> 
</bid-header> 
</title-code> 
</project-bidder-information> 
</data> 
</xml-report> 
</report> 

</reports> 


'my Code ************ 

Dim xtr As New XmlTextReader("C:\Test2.xml") 
xtr.WhitespaceHandling = WhitespaceHandling.None 

Dim X As New XmlDocument() 
X.Load(xtr) 

If Not (X Is Nothing) Then 
Try 
Dim ArticleList As XmlNodeList = X.SelectNodes("reports/report/xml-report") 
For Each Article As XmlNode In ArticleList 

Response.Write("**************New Record**********") 
Response.Write("<br />") 

Try 
Dim CatNodesList As XmlNodeList = Article.SelectNodes("summary/dr-nbr") 
Dim ProjectID As String 
For Each category As XmlNode In CatNodesList 
If category IsNot Nothing Then 
Response.Write("Project No:" & category.InnerText) 
ProjectID = category.InnerText 
Response.Write("<br />") 

End If 
Next 

Dim ProjectBidCompanyNodsList As XmlNodeList = Article.SelectNodes("data/project-bidder-informaton/title-code/bid-header/bid-title/bid-details/contact-information/firm-name") 
For Each ProjCompanyNode As XmlNode In ProjectBidCompanyNodsList 

If ProjCompanyNode IsNot Nothing Then 
Response.Write("Bid Company:" & ProjCompanyNode.Value) 
Response.Write("<br />") 
End If 
Next 

Catch ex As Exception 
Response.Write(ex) 
Response.Write("<br />") 
End Try 
Next 
Catch ex As Exception 
Response.Write(ex) 
End Try 
End If 

我能拉項目編號(DR-NBR),但沒有中標的公司是在第二排,我在做什麼錯。

+0

你是如何試圖 '拉' 的投標人信息?郵政編碼。 – Dan675 2012-02-28 22:34:30

+0

在我剛剛出現的例外代碼中,是投標人信息的代碼,我無法在此處的評論中重新發布 – user745708 2012-02-28 22:57:05

+0

「巨​​大」一詞毫無意義。這可能意味着從5Mb到200Gb。在這個頻譜的不同端,答案可能會有所不同。 – 2012-02-29 09:02:24

回答

0

只需使用LINQ to XML。這很容易。 C#示例:

public class XmlParser 
{ 
    public void Parse(string xml) 
    { 
     XDocument xDocument = XDocument.Parse(xml); 
     XElement bidderInformation = xDocument.Descendants("project-bidder-information").FirstOrDefault(); 
     Console.WriteLine(bidderInformation); 
    } 
} 

產量:

<project-bidder-information> 
    <title-code> 
    <bid-header> 
     <bid-header-desc ci="N">Low Bidders</bid-header-desc> 
     <bid-title> 
     <bid-details> 
      <contact-information> 
      <firm-name>Many Stocks</firm-name> 
      <contact-name>Many Moree</contact-name> 
      </contact-information> 
     </bid-details> 
     <bid-details> 
      <contact-information> 
      <firm-name>Who Constrcution</firm-name> 
      </contact-information> 
     </bid-details> 
     </bid-title> 
    </bid-header> 
    </title-code> 
</project-bidder-information> 
+0

對不起,當我使用這個Linq到XML讀取文件時,它給了我錯誤在根級別的數據是無效的。第1行,位置1 – user745708 2012-02-29 14:27:09

+0

您需要確保您傳遞格式良好的XML。 – 2012-02-29 18:04:05

+0

這是一個第三方文件,他們說xml格式正確,我能做些什麼來解決這個問題,我非常感謝你的時間。還有如何獲得dr-nbr(ProjectNO)和公司名稱1和公司名稱2 ..依次爲 – user745708 2012-02-29 19:37:00