2016-11-17 21 views
0

下午好,不能返回XML格式的字符串,使用C#和DB模板StringBuilder的,對於一個佔位符

我有建立一個字符串在我的數據庫模板輸出的XML結構的功能。它需要一個Order類作爲參數和模板,以及我有麻煩試圖填充名爲[[items]]的佔位符標籤,其中根據表中的項目數量,它應該爲該部分構建xml列表。

這是模板,它只返回字符串的一個實例,但它應該基於表中的內容返回(n)個實例。

<Items> 
    [[items]] 
</Items> 

這是應該填充爲其他標記的項目

<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> 

,它要填充像這樣

<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: This book was donated to CAEO Font: Arial 
       Custom Text Line 2 Text: In tribute to & 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 with up To 4 Lines of Custom Text]]></Description> 
    <Attributes><![CDATA[Custom Text Line 1 Text: This book was donated to CAEO Font: Arial 
       Custom Text Line 2 Text: In tribute to & memory of Font: Arial]]></Attributes> 
    <Quantity>1</Quantity> 
    <UnitPrice><![CDATA[$8.99]]></UnitPrice> 
    <InkColor>Blue</InkColor> 
    </Item> 
</Items> 

這裏是爲了做到這一點

功能
public string PopulateStringBuilder(Order order, Template template) 
    { 
     var i = 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(); 
    } 

回答

1

嘗試doi正確的方式

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: This book was donated to CAEO Font: Arial" + 
          " Custom Text Line 2 Text: In tribute to & memory of Font: Arial", 
        Quantity = 1, 
        UnitPrice = "$9.99", 
        InkColor = "Red" 
       }, 
       new Item() { 
        SKU = "42A1848", 
        PROD_NAME = "Test Two", 
        Description = "Self Inking Rubber Stamp with up To 4 Lines of Custom Text", 
        Attributes = "Custom Text Line 1 Text: This book was donated to CAEO Font: Arial" + 
          " Custom Text Line 2 Text: In tribute to & 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: This book was donated to CAEO Font: Arial 
//    Custom Text Line 2 Text: In tribute to & 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 with up To 4 Lines of Custom Text]]></Description> 
// <Attributes><![CDATA[Custom Text Line 1 Text: This book was donated to CAEO Font: Arial 
//    Custom Text Line 2 Text: In tribute to & memory of Font: Arial]]></Attributes> 
// <Quantity>1</Quantity> 
// <UnitPrice><![CDATA[$8.99]]></UnitPrice> 
// <InkColor>Blue</InkColor> 
// </Item> 
//</Items> 
相關問題