Link to home
Start Free TrialLog in
Avatar of Tom Sage
Tom SageFlag for United States of America

asked on

XML XDocument extract - Keeping a node list like a menu path

Here is an example XML I am reading with XDocument.
<Auto>
<Model>FORD</Model>
<ModelYear>2013</ModelYear>
<ModelDescription>PICKUP</ModelDescription>
<Make>F-150</Make>
<SaleClass>NEW</SaleClass>
<VIN>5N1995512300</VIN>
<DeliveryMileage uom="M">1000</DeliveryMileage>
<AutoStock>DT</AutoStock>
<BodyStyle>PICKUP</BodyStyle>
<ExteriorColor>WHITE</ExteriorColor>
<Pricing>
	<AutoPrice currency="USD">20000.34</AutoPrice>
	<AutoPricingType>Net Cap Cost</AutoPricingType>
</Pricing>
<Pricing>
	<AutoPrice currency="USD">21000.89</AutoPrice>
	<AutoPricingType>Gross Cap Cost</AutoPricingType>
</Pricing>
<CollateralType>C</CollateralType>
<NumberOfEngineCylinders>8</NumberOfEngineCylinders>
<VanConversionCost>0.00</VanConversionCost>
<AutoUse>Personal</AutoUse>
</Auto>

Open in new window


Here is what I would like for the output to be.   This is similar to a menu path except using period separators.

Auto.Model
Auto.ModelYear
Auto.ModelDescription
Auto.Make
Auto.SaleClass
Auto.VIN
Auto.DeliveryMileage
Auto.AutoStock
Auto.BodyStyle
Auto.ExteriorColor
Auto.Pricing.AutoPrice
Auto.Pricing.AutoPricingType
Auto.Pricing.AutoPrice
Auto.Pricing.AutoPricingType
Auto.CollateralType
Auto.NumberOfEngineCylinders
Auto.VanConversionCost
Auto.AutoUse

Open in new window


Here is my code as a starting place:
For Each e As XElement In xdoc.Descendants
   Console.Writeline(e.Name.LocalName & "," & e.Value)
Next

Open in new window


Thanks for any ideas
Avatar of ste5an
ste5an
Flag of Germany image

You may use the Parent property of XElement..

Something like pseudo-code:
Function FullPath(ANode) As String
  If ANode.Parent Is Nothing 
    FullPath = ANode.Name.LocalName
  Else
    FullPath = FullPath(ANode.Parent) & "." & ANode.Name.LocalName
  End If
End Function

Open in new window

Avatar of Tom Sage

ASKER

Hello,

Anode.Parent  shows the row in XML format.  I want just the text, like this:

Auto.Pricing.AutoPricingType

Thanks
D'oh?

How did you implement my pseudo-code function? How did you invoke it?

Maybe it's only

Function FullPath(ANode) As String
  If ANode.Parent Is Nothing 
    FullPath = ANode.Name
  Else
    FullPath = FullPath(ANode.Parent) & "." & ANode.Name
  End If
End Function

Open in new window

Here is my test code

Dim doc As XDocument = XDocument.Load("my.xml")

            For Each xel In doc.Descendants

                If xel.Parent Is Nothing Then
                    Console.WriteLine(xel.Name.LocalName & "," & xel.Value)
                Else
                    Console.WriteLine(xel.Parent.ToString & "." & xel.Name.LocalName & "," & xel.Value)
                End If

            Next

            Console.WriteLine("Done - press Enter")
            Console.ReadLine()

Open in new window


Is this correct ?

Thank you
No. You should implement the above function and use it as:

Dim doc As XDocument = XDocument.Load("my.xml")

For Each xel In doc.Descendants	
  Console.WriteLine(FullPath(xel))
Next

Console.WriteLine("Done - press Enter")
Console.ReadLine()

Open in new window

On line 4 of the code in the previous comment, I am getting this error:

Conversion from type 'XName' to type 'String' is not valid.
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Very good job - thank you for the help !!