Tom Sage
asked on
XML XDocument extract - Keeping a node list like a menu path
Here is an example XML I am reading with XDocument.
Here is what I would like for the output to be. This is similar to a menu path except using period separators.
Here is my code as a starting place:
Thanks for any ideas
<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>
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
Here is my code as a starting place:
For Each e As XElement In xdoc.Descendants
Console.Writeline(e.Name.LocalName & "," & e.Value)
Next
Thanks for any ideas
ASKER
Hello,
Anode.Parent shows the row in XML format. I want just the text, like this:
Auto.Pricing.AutoPricingTy pe
Thanks
Anode.Parent shows the row in XML format. I want just the text, like this:
Auto.Pricing.AutoPricingTy
Thanks
D'oh?
How did you implement my pseudo-code function? How did you invoke it?
Maybe it's only
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
ASKER
Here is my test code
Is this correct ?
Thank you
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()
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()
ASKER
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.
Conversion from type 'XName' to type 'String' is not valid.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Very good job - thank you for the help !!
Something like pseudo-code:
Open in new window