Question XML to CSV

Coder99

New member
Joined
Sep 15, 2019
Messages
1
Programming Experience
Beginner
My XML file.

1568586249315.png


Below is the code I am using to export my XML file to a CSV file. I can export the text MOISTURE, DOCKAGE, GRADE DISCOUNT etc. to column A. But I want to export the numerical values for these fields 14.5, 10.0, 0.0 etc. I don't know how to get one level deeper in the tree. Help?

C#:
namespace ConvertXML
{
    class Program
    {
        static void Main()
        {
            StringBuilder result = new StringBuilder();
            foreach (XElement level1Element in XElement.Load(@"C:\scaletickets.xml").Descendants("discount"))
            {
                result.Append(level1Element.Attribute("description").Value + "\r\n" );
            }
            
            StreamWriter sw = new StreamWriter(@"C:\Result.csv");
            sw.WriteLine(result.ToString());
            sw.Close();
        }
    }
}
 

Attachments

  • 1568586211046.png
    1568586211046.png
    41.6 KB · Views: 13
Last edited by a moderator:
level1Element.Attribute("description").Value refers to the Value of the "description" Attribute. To get the the value of the XElement, "14.5", you'll simply want level1Element.Value. See the documentation.
 
It is better if you paste your XML file on the forums as text wrapped in code tags, and not as a screenshot. At least this allows us to copy and paste its content should we want to try to replicate what you're doing or trying to do. We can't do that with a screenshot.
 
Back
Top Bottom