将XML字符串转换为CSV?

时间:2013-08-30 14:59:59

标签: xml vb.net csv

我有一个从第三方例程返回的XML字符串。我希望能够在我的VB.NET程序中将此XML字符串输出到.CSV文件中。有人可以就最好的方法提供一些建议吗?

由于

1 个答案:

答案 0 :(得分:2)

试试这个:

' First read your XML string in
Dim doc As XDocument = XDocument.Parse(xmlString)

' Create a string builder to hold the output CSV
Dim theOutput As New StringBuilder(1000)

' Loop through the nodes of the XML
For Each node As XElement In doc.Descendants("name of element you want to start at")
    For Each innerNode As XElement In node.Elements()
        theOutput.AppendFormat("{0},", innerNode.Attribute("data").Value)
    Next

    ' Remove trailing comma
    theOutput.Remove(theOutput.Length - 1, 1)
    theOutput.AppendLine()
Next

' Write output to file and do whatever you want the file here
File.WriteAllText("path to file.csv", theOutput.ToString())