将 XML 个标签转换为 CSV

Conversion of XML tags to CSV

我想将一些 XML 标签转换为逗号分隔值 (CSV)

    <variable name="Fault_Reset">
          <type>
            <BOOL />
          </type>
     </variable>
     <variable name="Cycle_On">
          <type>
            <BOOL />
          </type>
     </variable>

我希望输出如下所示: 变量名,类型

例如

Fault_Reset,BOOL

Cycle_On,BOOL

请帮帮我。

喜欢对这种类型的代码使用字典。我认为 BOOL 应该是 innertext 而不是标签名称。尝试下面的代码,它适用于 XML posted

using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication132
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            Dictionary<string, string> dict = doc.Descendants("variable")
                .GroupBy(x => (string)x.Attribute("name"), y => (string)y.Element("type").Elements().FirstOrDefault().Name.LocalName)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());

        }
    }


}