从巨大的xml文件中的可选XML节点读取值

时间:2012-02-28 22:32:50

标签: asp.net xml

我有一个巨大的XML文件,其中的一些节点是可选的。我无法从中提取出价者信息,任何人都可以请求帮助。 xml文件的前2个记录在下面给出

<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)而不是第二排的竞标公司,我做错了什么。

1 个答案:

答案 0 :(得分: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>