1

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

谢谢

4

1 回答 1

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())
于 2013-08-30T15:11:52.587 回答