简体   繁体   English

用VB.net解析XML

[英]Parsing XML with VB.net

I'm trying to make sense of a big data dump of XML that I need to write to a database using some VB.net code. 我试图弄清楚我需要使用一些VB.net代码写入数据库的XML大数据转储。 I'm looking for some help getting started with the parsing code, specifically how to access the attribute values. 我正在寻找有关解析代码入门的帮助,特别是如何访问属性值。

                  <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>

Basically I want to find a property within the xml file that has a certain property ID. 基本上,我想在xml文件中找到具有特定属性ID的属性。 So it would be something like the code listed below which is my interpretation of what it should be. 因此,它将类似于下面列出的代码,这是我对其应有的解释。 The code doesn't work, so I'm going wrong somewhere with it. 该代码无法正常工作,因此我在某处出错。

This is the relevant line that I need to be able to access: 这是我需要访问的相关行:

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

For parsing XML with VB.Net you need to use the System.XML namespace. 为了使用VB.Net解析XML,您需要使用System.XML命名空间。 This has all the tools you need to pass the XML in your question. 它具有在您的问题中传递XML所需的所有工具。

To query attributes, the following code will work (where xNode is an XmlNode object) 要查询属性,可以使用以下代码(其中xNodeXmlNode对象)

xNode.Attributes(attributeName).Value.ToString

If the attribute does not exist you will get Nothing returned 如果该属性不存在,则不会返回Nothing

The following looks like a nice tutorial 以下看起来像一个不错的教程

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

Maybe you could solve this with XPath 也许您可以使用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)

Glad to see you're able to use the Linq to XML features in your query. 很高兴看到您能够在查询中使用Linq to XML功能。 It will make this much easier. 这将使此过程变得更加容易。 Building on what you had, here is something that may do what you're asking: 在您拥有的基础上,以下内容可能会满足您的要求:

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