I need to parse some monster XML docs and have had a play with Linq to XML this morning - not used it before but looks good!
Can anyone point me in the right direction here, say i've got a simple XML doc:
I want to find out what the Version attributes are so i try:
This returns:
Version Name: TheVersionName
Version Description: TheVersionDesc
Version Status: TheVersionNameWorking
What should my incorrect line 'VersionStatus = version.Element("Properties").Value' be so i can just grab the property value: name="Version Status" and get Version Status: Working? i.e i need to filter on the name value of the property attributes.
Ta.
Can anyone point me in the right direction here, say i've got a simple XML doc:
Code:
<?xml version="1.0" ?> <SomeMetadata xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Versions> <Version name="TheVersionName" description="TheVersionDesc"> <Properties> <Property name="Version Name">TheVersionName</Property> <Property name="Version Status">Working</Property> </Properties> <Hierarchies> <Hierarchy name="Period" description="Period Hierarchy"> <Properties> <Property name="Hier Name">Period</Property> <Property name="Hierarchy Status">Working</Property> </Properties> </Hierarchy> <Hierarchy name="Currency" description="Currency Hierarchy"> <Properties> <Property name="Hier Name">Currency</Property> <Property name="Hierarchy Status">Working</Property> </Properties> </Hierarchy> </Hierarchies> </Version> </Versions> </SomeMetadata>
Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim TheXmlDoc As New XDocument TheXmlDoc = XDocument.Load(Server.MapPath("XML/Test.xml")) Dim versions = From version In TheXmlDoc.Descendants("Version") _ Select VersionName = version.Attribute("name").Value, _ VersionDesc = version.Attribute("description").Value, _ VersionStatus = version.Element("Properties").Value For Each item In versions Response.Write("Version Name: " & item.VersionName & "<br />Version Description: " & item.VersionDesc _ & "<br />Version Status: " & item.VersionStatus) Next End Sub
Version Name: TheVersionName
Version Description: TheVersionDesc
Version Status: TheVersionNameWorking
What should my incorrect line 'VersionStatus = version.Element("Properties").Value' be so i can just grab the property value: name="Version Status" and get Version Status: Working? i.e i need to filter on the name value of the property attributes.
Ta.
Comment