xml - C# foreach loop outer string not updating as normal -
i know that's it's better write xml documents proper class i'm short in time , want quick result.
i tried write xml file in string save xml file. result not correct, because string getting last value of foreach loop every time.
i searched problem , found out need initiate string inside foreach loop, didn't understand concept , didn't know exactly.
what should modify in code?
string sitemap = "<? xml version = \"1.0\" encoding = \"utf-8\" ?>"; list<string> niveau1titles = getniveau1titles(aux1); list<list<couple>> niveau2titles = getniveau2titles(aux1); int = 0; foreach (var n2 in niveau2titles) { sitemap += "<niveau_1 title = \"" + niveau1titles[i] + "\" >"; foreach (var n22 in n2) { sitemap += "<niveau_2 title = \"" + n22.title + "\" link = \"" + n22.link + "\" >"; list<couple> niveau3titles = new list<couple>(); niveau3titles = getniveau3titles(n22.link); foreach(var n3 in niveau3titles) { sitemap += "<niveau_3 title = \"" + n3.title + "\" link = \"" + n3.link + "\" />"; } sitemap += "</niveau_2>"; } sitemap += "</niveau_1>"; i++; } console.writeline(sitemap);
i think code working correctly. have multiple root elements in xml. need add root. have space in xml identification line. try code below.
using system; using system.collections.generic; using system.linq; using system.text; namespace consoleapplication1 { class program { static void main(string[] args) { string aux1 = ""; //remove space string sitemap = "<?xml version = \"1.0\" encoding = \"utf-8\" ?>"; sitemap += "<root>"; list<string> niveau1titles = getniveau1titles(aux1); list<list<couple>> niveau2titles = getniveau2titles(aux1); int = 0; foreach (var n2 in niveau2titles) { sitemap += "<niveau_1 title = \"" + niveau1titles[i] + "\" >"; foreach (var n22 in n2) { sitemap += "<niveau_2 title = \"" + n22.title + "\" link = \"" + n22.link + "\" >"; list<couple> niveau3titles = new list<couple>(); niveau3titles = getniveau3titles(n22.link); foreach (var n3 in niveau3titles) { sitemap += "<niveau_3 title = \"" + n3.title + "\" link = \"" + n3.link + "\" />"; } sitemap += "</niveau_2>"; } sitemap += "</niveau_1>"; i++; } sitemap += "</root>"; console.writeline(sitemap); console.readline(); } static list<string> getniveau1titles(string aux1) { return new list<string> { "aaa", "aab", "aac", "aad" }; } static list<list<couple>> getniveau2titles(string aux1) { return new list<list<couple>>() { new list<couple>() {new couple() { title = "t11", link = "l11"}, new couple() { title = "t12", link = "l12"}, new couple() { title = "t13", link = "l13"} }, new list<couple>() {new couple() { title = "t21", link = "l21"}, new couple() { title = "t22", link = "l22"}, new couple() { title = "t23", link = "l23"} }, new list<couple>() {new couple() { title = "t31", link = "l31"}, new couple() { title = "t32", link = "l32"}, new couple() { title = "t33", link = "l33"} }, new list<couple>() {new couple() { title = "t41", link = "l41"}, new couple() { title = "t42", link = "l42"}, new couple() { title = "t43", link = "l43"} } }; } static list<couple> getniveau3titles(string aux1) { return new list<couple>() { new couple() { title = "t100", link = "l100" }, new couple() { title = "t101", link = "l101" }, new couple() { title = "t102", link = "l102" } }; } } public class couple { public string title { get; set; } public string link { get; set; } } }
here xml results
<?xml version = "1.0" encoding = "utf-8" ?> <root> <niveau_1 title = "aaa" > <niveau_2 title = "t11" link = "l11" > <niveau_3 title = "t100" link = "l100" /> <niveau_3 title = "t101" link = "l101" /> <niveau_3 title = "t102" link = "l102" /> </niveau_2> <niveau_2 title = "t12" link = "l12" > <niveau_3 title = "t100" link = "l100" /> <niveau_3 title = "t101" link = "l101" /> <niveau_3 title = "t102" link = "l102" /> </niveau_2> <niveau_2 title = "t13" link = "l13" > <niveau_3 title = "t100" link = "l100" /> <niveau_3 title = "t101" link = "l101" /> <niveau_3 title = "t102" link = "l102" /> </niveau_2> </niveau_1> <niveau_1 title = "aab" > <niveau_2 title = "t21" link = "l21" > <niveau_3 title = "t100" link = "l100" /> <niveau_3 title = "t101" link = "l101" /> <niveau_3 title = "t102" link = "l102" /> </niveau_2> <niveau_2 title = "t22" link = "l22" > <niveau_3 title = "t100" link = "l100" /> <niveau_3 title = "t101" link = "l101" /> <niveau_3 title = "t102" link = "l102" /> </niveau_2> <niveau_2 title = "t23" link = "l23" > <niveau_3 title = "t100" link = "l100" /> <niveau_3 title = "t101" link = "l101" /> <niveau_3 title = "t102" link = "l102" /> </niveau_2> </niveau_1> <niveau_1 title = "aac" > <niveau_2 title = "t31" link = "l31" > <niveau_3 title = "t100" link = "l100" /> <niveau_3 title = "t101" link = "l101" /> <niveau_3 title = "t102" link = "l102" /> </niveau_2> <niveau_2 title = "t32" link = "l32" > <niveau_3 title = "t100" link = "l100" /> <niveau_3 title = "t101" link = "l101" /> <niveau_3 title = "t102" link = "l102" /> </niveau_2> <niveau_2 title = "t33" link = "l33" > <niveau_3 title = "t100" link = "l100" /> <niveau_3 title = "t101" link = "l101" /> <niveau_3 title = "t102" link = "l102" /> </niveau_2> </niveau_1> <niveau_1 title = "aad" > <niveau_2 title = "t41" link = "l41" > <niveau_3 title = "t100" link = "l100" /> <niveau_3 title = "t101" link = "l101" /> <niveau_3 title = "t102" link = "l102" /> </niveau_2> <niveau_2 title = "t42" link = "l42" > <niveau_3 title = "t100" link = "l100" /> <niveau_3 title = "t101" link = "l101" /> <niveau_3 title = "t102" link = "l102" /> </niveau_2> <niveau_2 title = "t43" link = "l43" > <niveau_3 title = "t100" link = "l100" /> <niveau_3 title = "t101" link = "l101" /> <niveau_3 title = "t102" link = "l102" /> </niveau_2> </niveau_1> </root>
Comments
Post a Comment