Can't return an XML formatted string, using stringbuilder in C# and a DB Template, For a PlaceHolder -
good afternoon,
i have function builds string template in db outputs xml structure. takes in order class parameter , template, having troubles trying populate placeholder tag named [[items]], based on number of items in table should build xml list portion.
this template, returning 1 instance of string should return (n) instances based on what's in table.
<items> [[items]] </items>
this should populated other tags item
<item> <sku>[[item_sku]]</sku> <prod_name><![cdata[[[item_product_name]]]]></prod_name> <description><![cdata[[[item_name]]]]></description> <attributes><![cdata[[[item_attributes]]]]></attributes> <quantity>[[item_qty]]</quantity> <unitprice>[[item_price]]</unitprice> <inkcolor>[[item_inkcolor]]</inkcolor> </item>
and want populate this
<items> <item> <sku>7z-brpa-k79a</sku> <prod_name><![cdata[test one]]></prod_name> <description><![cdata[excelmark 5-line large return address stamp - custom self inking rubber stamp]]></description> <attributes><![cdata[custom text line 1 text: book donated caeo font: arial custom text line 2 text: in tribute & memory of font: arial]]></attributes> <quantity>1</quantity> <unitprice><![cdata[$9.99]]></unitprice> <inkcolor>red</inkcolor> </item> <item> <sku>42a1848</sku> <prod_name><![cdata[test two]]></prod_name> <description><![cdata[self inking rubber stamp 4 lines of custom text]]></description> <attributes><![cdata[custom text line 1 text: book donated caeo font: arial custom text line 2 text: in tribute & memory of font: arial]]></attributes> <quantity>1</quantity> <unitprice><![cdata[$8.99]]></unitprice> <inkcolor>blue</inkcolor> </item> </items>
here function make happen
public string populatestringbuilder(order order, template template) { var = new stringbuilder(template.description); list<string> s = new list<string>(); var reqid = order.orderid; foreach (var x in order.items) { i.replace("[[item_sku]]", x.skunumber); i.replace("[[item_product_name]]", x.productname); i.replace("[[item_name]]", x.description); i.replace("[[item_attributes]]", x.attributes); i.replace("[[item_qty]]", x.quantity.tostring()); i.replace("[[item_price]]", x.unitprice.tostring()); i.replace("[[item_inkcolor]]", x.inkcolor); o.replace("[[items]]", i.tostring()); } return o.tostring(); }
try doing correct way
using system; using system.collections.generic; using system.linq; using system.text; using system.xml; using system.xml.linq; namespace consoleapplication1 { class program { static void main(string[] args) { list<item> items = new list<item>() { new item() { sku = "7z-brpa-k79a", prod_name = "test one", description = "excelmark 5-line large return address stamp - custom self inking rubber stamp", attributes = "custom text line 1 text: book donated caeo font: arial" + " custom text line 2 text: in tribute & memory of font: arial", quantity = 1, unitprice = "$9.99", inkcolor = "red" }, new item() { sku = "42a1848", prod_name = "test two", description = "self inking rubber stamp 4 lines of custom text", attributes = "custom text line 1 text: book donated caeo font: arial" + " custom text line 2 text: in tribute & memory of font: arial", quantity = 1, unitprice = "$8.99", inkcolor = "blue" } }; xelement xitems = new xelement("items"); foreach (item item in items) { xelement xitem = new xelement("item", new object[] { new xelement("sku", item.sku), new xelement("prod_name", new xcdata(item.prod_name)), new xelement("description", new xcdata(item.description)), new xelement("attribute", new xcdata(item.attributes)), new xelement("quantity", item.quantity), new xelement("unitprice", new xcdata(item.unitprice)), new xelement("inkcolor", item.inkcolor) }); xitems.add(xitem); } } } public class item { public string sku { get; set; } public string prod_name { get; set; } public string description { get; set; } public string attributes { get; set; } public int quantity { get; set; } public string unitprice { get; set; } public string inkcolor { get; set; } } } //<items> // <item> // <sku>7z-brpa-k79a</sku> // <prod_name><![cdata[test one]]></prod_name> // <description><![cdata[excelmark 5-line large return address stamp - custom self inking rubber stamp]]></description> // <attributes><![cdata[custom text line 1 text: book donated caeo font: arial // custom text line 2 text: in tribute & memory of font: arial]]></attributes> // <quantity>1</quantity> // <unitprice><![cdata[$9.99]]></unitprice> // <inkcolor>red</inkcolor> // </item> // <item> // <sku>42a1848</sku> // <prod_name><![cdata[test two]]></prod_name> // <description><![cdata[self inking rubber stamp 4 lines of custom text]]></description> // <attributes><![cdata[custom text line 1 text: book donated caeo font: arial // custom text line 2 text: in tribute & memory of font: arial]]></attributes> // <quantity>1</quantity> // <unitprice><![cdata[$8.99]]></unitprice> // <inkcolor>blue</inkcolor> // </item> //</items>
Comments
Post a Comment