簡體   English   中英

用VB.net解析XML

[英]Parsing XML with VB.net

我試圖弄清楚我需要使用一些VB.net代碼寫入數據庫的XML大數據轉儲。 我正在尋找有關解析代碼入門的幫助,特別是如何訪問屬性值。

                  <Product ID="523233" UserTypeID="Property" ParentID="523232">
                <Name>My Property Name</Name>                     
                <AssetCrossReference AssetID="173501" Type=" Non Print old">
                  </AssetCrossReference>
                  <AssetCrossReference AssetID="554740" Type=" Non Print old">
                  </AssetCrossReference>
                  <AssetCrossReference AssetID="566495" Type=" Non Print old">
                  </AssetCrossReference>
                  <AssetCrossReference AssetID="553014" Type="Non Print">
                  </AssetCrossReference>
                  <AssetCrossReference AssetID="553015" Type="Non Print">
                  </AssetCrossReference>
                  <AssetCrossReference AssetID="553016" Type="Non Print">
                  </AssetCrossReference>
                  <AssetCrossReference AssetID="553017" Type="Non Print">
                  </AssetCrossReference>
                  <AssetCrossReference AssetID="553018" Type="Non Print">
                  </AssetCrossReference>

                <Values>
                  <Value AttributeID="5115">Section of main pool</Value>
                  <Value AttributeID="5137">114 apartments, four floors, no lifts</Value>
                  <Value AttributeID="5170">Property location</Value>
                  <Value AttributeID="5164">2 key</Value>
                  <Value AttributeID="5134">A comfortable property, the apartment is set on a pine-covered hillside - a scenic and peaceful location.</Value>
                  <Value AttributeID="5200">PROPERTY_ID</Value>
                  <Value AttributeID="5148">facilities include X,Y,Z</Value>
                  <Value AttributeID="5067">Self Catering. </Value>
                  <Value AttributeID="5221">Frequent organised daytime activities</Value>
                </Values>
              </Product>
            </Product>

基本上,我想在xml文件中找到具有特定屬性ID的屬性。 因此,它將類似於下面列出的代碼,這是我對其應有的解釋。 該代碼無法正常工作,因此我在某處出錯。

這是我需要訪問的相關行:

<Value AttributeID="5200">PROPERTY_ID</Value>


Dim productsXML As XElement = XElement.Load("C:\myFile.xml")

Dim foundNode As XElement

Dim query = From p In productsXML.Elements("Product").Descendants("Values") Where p.Attributes("attribute").ToString = "PROPERTY_ID"

foundNode = query.FirstOrDefault()

為了使用VB.Net解析XML,您需要使用System.XML命名空間。 它具有在您的問題中傳遞XML所需的所有工具。

要查詢屬性,可以使用以下代碼(其中xNodeXmlNode對象)

xNode.Attributes(attributeName).Value.ToString

如果該屬性不存在,則不會返回Nothing

以下看起來像一個不錯的教程

http://www.beansoftware.com/ASP.NET-Tutorials/XML-Programming-VB.NET.aspx

也許您可以使用XPath解決此問題

Dim document As XPathDocument = New XPathDocument("products.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

Dim node As XPathNavigator = navigator.SelectSingleNode("//Product/Values/Value[@AttributeID='your id']")
Console.WriteLine(node.InnerXml)

很高興看到您能夠在查詢中使用Linq to XML功能。 這將使此過程變得更加容易。 在您擁有的基礎上,以下內容可能會滿足您的要求:

Dim rootEl As XElement = XDocument.Load("C:\myFile.xml").Root
Dim propertyEl = (From p In rootEl.Descendants("Product") Where p.Attributes("ID").Value = "PROPERTY_ID").FirstOrDefault()
' Now that you have the property element, you can query it.
Dim query = From p In propertyEl Where p.Descendants("Value").Attribute("AttributeID").Value = "ATTR_ID")
' Or just loop through it
For Each el As XElement In propertyEl.Descendants("Value")
   'Do something
Next

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM