【问题标题】:Parsing XML with VB.net用 VB.net 解析 XML
【发布时间】:2010-07-19 19:33:58
【问题描述】:

我试图理解需要使用一些 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()

【问题讨论】:

  • 我可以使用下面的代码找到一个单独的节点,但我需要找到一种方法来遍历属性值,以便我可以将它们读入一个对象。 Dim productsXML As XElement = XElement.Load(XMLFile) Dim ParentNode As XElement Dim ChildNode As XElement Dim Query = From p In productsXML... _ Where p.Value = "PROPERTY_ID" ParentNode = Query.FirstOrDefault()跨度>

标签: xml vb.net


【解决方案1】:

要使用 VB.Net 解析 XML,您需要使用 System.XML 命名空间。这包含在您的问题中传递 XML 所需的所有工具。

要查询属性,可以使用以下代码(其中xNodeXmlNode 对象)

xNode.Attributes(attributeName).Value.ToString

如果属性不存在,你会得到Nothingreturned

下面的教程看起来不错

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

【讨论】:

    【解决方案2】:

    也许你可以用 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)
    

    【讨论】:

      【解决方案3】:

      很高兴看到您能够在查询中使用 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多