{"id":22482,"date":"2024-10-19T08:15:57","date_gmt":"2024-10-19T08:15:57","guid":{"rendered":"http:\/\/www.withdata.com\/blog\/?p=22482"},"modified":"2024-10-19T08:15:57","modified_gmt":"2024-10-19T08:15:57","slug":"convert-xml-mixed-nodes-and-attributes-to-csv-file","status":"publish","type":"post","link":"https:\/\/www.withdata.com\/blog\/use\/convert-xml-mixed-nodes-and-attributes-to-csv-file.html","title":{"rendered":"Convert XML (mixed nodes and attributes) to CSV file"},"content":{"rendered":"<p>I see a post <a href=\"https:\/\/stackoverflow.com\/questions\/61217342\/convert-xml-mixed-nodes-and-attributes-to-csv-file-using-c-sharp\">Convert xml (mixed nodes and attributes) to csv file using c#<\/a>, the poster has an XML which has mix of nodes and nodes with attributes:<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;Root&gt; \r\n  &lt;StudentRequestList count=\"3\"&gt;\r\n    &lt;StudentRequest&gt;\r\n        &lt;StudentFirstName&gt;Test1&lt;\/StudentFirstName&gt;\r\n        &lt;StudentLastName&gt;TestLastName&lt;\/StudentLastName&gt;\r\n        &lt;StudentGrade&gt;3&lt;\/StudentGrade&gt;\r\n        &lt;StudentHomeroomTeacher&gt;WhiteTest1&lt;\/StudentHomeroomTeacher&gt;\r\n        &lt;VariableData&gt;\r\n            &lt;Variable name=\"Email\"&gt;test@email.com&lt;\/Variable&gt;\r\n            &lt;Variable name=\"Hobby1\"&gt;TestHobby1&lt;\/Variable&gt;\r\n            &lt;Variable name=\"Hobby2\"&gt;TestHobby2&lt;\/Variable&gt;\r\n            &lt;Variable name=\"Hobby3\"&gt;TestHobby3&lt;\/Variable&gt;\r\n            &lt;Variable name=\"satscore\"&gt;satscoreTest1&lt;\/Variable&gt;\r\n            &lt;Variable name=\"kprepscore\"&gt;krepscore1&lt;\/Variable&gt;           \r\n        &lt;\/VariableData&gt;\r\n    &lt;\/StudentRequest&gt;\r\n        &lt;StudentRequest&gt;\r\n        &lt;StudentFirstName&gt;Test2&lt;\/StudentFirstName&gt;\r\n        &lt;StudentLastName&gt;TestLastName2&lt;\/StudentLastName&gt;\r\n        &lt;StudentGrade&gt;3&lt;\/StudentGrade&gt;\r\n        &lt;StudentHomeroomTeacher&gt;WhiteTest1&lt;\/StudentHomeroomTeacher&gt;\r\n        &lt;VariableData&gt;\r\n            &lt;Variable name=\"Email\"&gt;test2@email.com&lt;\/Variable&gt;\r\n            &lt;Variable name=\"Hobby5\"&gt;TestHobby5&lt;\/Variable&gt;\r\n            &lt;Variable name=\"Hobby6\"&gt;TestHobby6&lt;\/Variable&gt;\r\n            &lt;Variable name=\"Hobby3\"&gt;TestHobby3&lt;\/Variable&gt;\r\n            &lt;Variable name=\"satscore\"&gt;satscoreTest2&lt;\/Variable&gt;\r\n            &lt;Variable name=\"kprepscore\"&gt;krepscore2&lt;\/Variable&gt;           \r\n        &lt;\/VariableData&gt;\r\n    &lt;\/StudentRequest&gt;\r\n        &lt;StudentRequest&gt;\r\n        &lt;StudentFirstName&gt;Test1&lt;\/StudentFirstName&gt;\r\n        &lt;StudentLastName&gt;TestLastName&lt;\/StudentLastName&gt;\r\n        &lt;StudentGrade&gt;3&lt;\/StudentGrade&gt;\r\n        &lt;StudentHomeroomTeacher&gt;WhiteTest1&lt;\/StudentHomeroomTeacher&gt;\r\n        &lt;VariableData&gt;\r\n            &lt;Variable name=\"Email\"&gt;test@email.com&lt;\/Variable&gt;\r\n            &lt;Variable name=\"Hobby4\"&gt;TestHobby4&lt;\/Variable&gt;\r\n            &lt;Variable name=\"Hobby2\"&gt;TestHobby2&lt;\/Variable&gt;\r\n            &lt;Variable name=\"Hobby3\"&gt;TestHobby3&lt;\/Variable&gt;\r\n            &lt;Variable name=\"satscore\"&gt;satscoreTest3&lt;\/Variable&gt;\r\n            &lt;Variable name=\"kprepscore\"&gt;krepscore3&lt;\/Variable&gt;           \r\n        &lt;\/VariableData&gt;\r\n    &lt;\/StudentRequest&gt;\r\n  &lt;\/StudentRequestList&gt;\r\n&lt;\/Root&gt;\r\n<\/pre>\n<p>CSV is supposed to look like:<\/p>\n<pre>StudentFirstName,StudentLastName,StudentGrade,StudentHomeroomTeacher,Email, Hobby1, Hobby2,Hobby3, Hobby4, Hobby5, Hobby6, satscore, kprepscore     \r\nTest1,TestLastName,3,WhiteTest1,test@email.com,TestHobby1,TestHobby2,TestHobby3,,,,satscoreTest1,krepscore1 \r\nTest2,TestLastName2,3,WhiteTest1,test2@email.com,,,Hobby3,,Hobby5,Hobby6,satscoreTest2,krepscore2\r\nTest1,TestLastName,3,WhiteTest1,test@email.com,,Hobby2,Hobby3,Hobby4,,,satscoreTest3,krepscore3\r\n<\/pre>\n<p>I think <a href=\"https:\/\/www.withdata.com\/data-file-converter\/\" target=\"_blank\" rel=\"noopener\">Data File Converter<\/a> can do this, need to coding, so I tried, yes, it can, in a minute.<\/p>\n<p>1. Open XML file in DataFileConverter, set XML options.<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.withdata.com\/images\/case\/mixed_nodes_and_attributes_xml.png\" alt=\"Convert XML (mixed nodes and attributes) to CSV - open XML\" width=\"722\" height=\"572\" style=\"max-width:100%;height:auto;display:block;\" \/><\/p>\n<p>2. Set XML custom columns.<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.withdata.com\/images\/case\/mixed_nodes_and_attributes_custom_columns.png\" alt=\"Convert XML (mixed nodes and attributes) to CSV - set custom_coumns\" width=\"602\" height=\"465\" style=\"max-width:100%;height:auto;display:block;\" \/><\/p>\n<p>3. Set CSV options.<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.withdata.com\/images\/case\/mixed_nodes_and_attributes_csv.png\" alt=\"Convert XML (mixed nodes and attributes) to CSV - set CSV\" width=\"722\" height=\"572\" style=\"max-width:100%;height:auto;display:block;\" loading=\"lazy\" decoding=\"async\" \/><\/p>\n<p>4. &#8220;Next&#8221; and &#8220;Next&#8221;, convert, get output CSV file.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I see a post Convert xml (mixed nodes and attributes) to csv file using c#, the poster has an XML which has mix of nodes and nodes with attributes: &lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;utf-8&#8243;?&gt; &lt;Root&gt; &lt;StudentRequestList count=&#8221;3&#8243;&gt; &lt;StudentRequest&gt; &lt;StudentFirstName&gt;Test1&lt;\/StudentFirstName&gt; &lt;StudentLastName&gt;TestLastName&lt;\/StudentLastName&gt; &lt;StudentGrade&gt;3&lt;\/StudentGrade&gt; &lt;StudentHomeroomTeacher&gt;WhiteTest1&lt;\/StudentHomeroomTeacher&gt; &lt;VariableData&gt; &lt;Variable name=&#8221;Email&#8221;&gt;test@email.com&lt;\/Variable&gt; &lt;Variable name=&#8221;Hobby1&#8243;&gt;TestHobby1&lt;\/Variable&gt; &lt;Variable name=&#8221;Hobby2&#8243;&gt;TestHobby2&lt;\/Variable&gt; &lt;Variable name=&#8221;Hobby3&#8243;&gt;TestHobby3&lt;\/Variable&gt; &lt;Variable name=&#8221;satscore&#8221;&gt;satscoreTest1&lt;\/Variable&gt; &lt;Variable name=&#8221;kprepscore&#8221;&gt;krepscore1&lt;\/Variable&gt; &lt;\/VariableData&gt; &lt;\/StudentRequest&gt; [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3141],"tags":[34,68,1491],"class_list":["post-22482","post","type-post","status-publish","format-standard","hentry","category-use","tag-csv","tag-xml","tag-xml-to-csv"],"_links":{"self":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts\/22482","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/comments?post=22482"}],"version-history":[{"count":1,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts\/22482\/revisions"}],"predecessor-version":[{"id":22483,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts\/22482\/revisions\/22483"}],"wp:attachment":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/media?parent=22482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/categories?post=22482"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/tags?post=22482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}