Create XML File With Dynamic Sub Elements Level In C# -
there tree view in c# project contains dynamic level of categories , products belong 1 category
application create parent category single level of sub category
so need create xml file multiple sub categories
code :
private void createxmlfile(object sender, eventargs e) { try { list<prg.createxml.category> categories = service.category.instance.findall(); list<prg.createxml.product> products = service.product.instance.findall(); string filename = "category.xml"; string filepath = directory.getcurrentdirectory() + "\\data\\" + filename; if (file.exists(filepath)) file.delete(filepath); xmltextwriter writer = new xmltextwriter(filepath, system.text.encoding.utf8); writer.writestartdocument(true); writer.formatting = formatting.indented; writer.indentation = 3; writer.writestartelement("data"); createxmlcategoryfirstlevel(writer, categories, products); writer.writeendelement(); writer.writeenddocument(); writer.close(); createxmlproductdetails(products); messagebox.show("create xml file success!\nthe file address:\n" + directory.getcurrentdirectory() + "\\data"); } catch (exception ex) { messagebox.show("error in creat xml file. " + ex.message); } } private void createxmlcategoryfirstlevel(xmltextwriter writer, list<prg.createxml.category> categories, list<prg.createxml.product> products) { try { foreach (prg.createxml.category category in categories) { if (category.parentid == null) { writer.writestartelement("category"); writer.writestartelement("id"); writer.writestring(category.id.tostring()); writer.writeendelement(); writer.writestartelement("name"); writer.writestring(category.name); writer.writeendelement(); writer.writestartelement("image"); if (!string.isnullorempty(category.image)) { writer.writestring(category.image.replace(directory.getcurrentdirectory() + "\\", "")); } else { writer.writestring(""); } writer.writeendelement(); writer.writestartelement("description"); writer.writestring(category.description); writer.writeendelement(); createxmlsubcategory(writer, categories, category.id, products); writer.writeendelement(); } } } catch (exception ex) { throw ex; } } private void createxmlsubcategory(xmltextwriter writer, list<prg.createxml.category> categories, long parentid, list<prg.createxml.product> products) { try { foreach (prg.createxml.category category in categories) { if (category.parentid == parentid) { writer.writestartelement("subcategory"); writer.writestartelement("id"); writer.writestring(category.id.tostring()); writer.writeendelement(); writer.writestartelement("name"); writer.writestring(category.name); writer.writeendelement(); writer.writestartelement("image"); if (!string.isnullorempty(category.image)) { writer.writestring(category.image.replace(directory.getcurrentdirectory() + "\\", "")); } else { writer.writestring(""); } writer.writeendelement(); writer.writestartelement("description"); writer.writestring(category.description); writer.writeendelement(); writer.writestartelement("parentid"); writer.writestring(category.parentid.tostring()); writer.writeendelement(); // added two9 lines error occured // createxmlsubcategory(writer, categories, category.id, products); // writer.writeendelement(); createxmlproduct(writer, products, category.id); writer.writeendelement(); } } } catch (exception ex) { throw ex; } } private void createxmlproduct(xmltextwriter writer, list<prg.createxml.product> products, long categoryid) { try { foreach (prg.createxml.product product in products) { if (product.categoryid == categoryid) { writer.writestartelement("product"); writer.writestartelement("id"); writer.writestring(product.id.tostring()); writer.writeendelement(); writer.writestartelement("name"); writer.writestring(product.name); writer.writeendelement(); writer.writestartelement("categoryid"); writer.writestring(product.categoryid.tostring()); writer.writeendelement(); writer.writestartelement("image"); if (!string.isnullorempty(product.image)) { writer.writestring(product.image.replace(directory.getcurrentdirectory() + "\\", "")); } else { writer.writestring(""); } writer.writeendelement(); writer.writestartelement("description"); writer.writestring(product.description); writer.writeendelement(); writer.writeendelement(); } } } catch (exception ex) { throw ex; } }
problem solved
createxmlsubcategory(writer, categories, category.id, products); // writer.writeendelement();
Comments
Post a Comment