2010-04-02 88 views
0

我试图理解需要使用一些VB.net代码写入数据库的XML的大数据转储。我正在寻找解析代码入门的一些帮助,特别是如何访问属性值。用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> 

基本上我想在具有特定属性ID的xml文件中找到属性。所以它会像下面列出的代码一样,这是我对它应该是什么的解释。该代码不起作用,所以我会在某处出错。

这是相关行,我需要能够访问:

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

我能找到一个单独的节点使用下面的代码,但我需要找出一种方法来遍历属性值所以我可以将它们读入一个对象。 昏暗productsXML作为的XElement = XElement.Load(XMLFILE) 昏暗ParentNode作为的XElement 昏暗ChildNode作为的XElement 昏暗查询=从p在productsXML ... _ 凡p.Value = “PROPERTY_ID” ParentNode =查询。 FirstOrDefault() – SuperFurryToad 2010-04-06 20:10:47

回答

1

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

0

也许你会找到d用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) 
0

很高兴解决这个问题,看看你能使用LINQ到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