DateSet结果集转成UnicodeXml格式

DateSet结果集转成UnicodeXml格式

    public static string ToUnicodeXML(DataSet ds)
    {
            //if (PublicClass.HasRecord(ds, false)==false)
            //return "";
            System.Text.StringBuilder StrBuilder = new System.Text.StringBuilder();
            UnicodeEncoding UTF = new UnicodeEncoding();
            //string Result = "<?xml version=\"1.0\" standalone=\"yes\"?>";
            StrBuilder.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            //string Result = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
            //Result += "<DATAPACKET Version=\"2.0\">";
            //Result += "<METADATA><FIELDS>";  
            //StrBuilder.Append("<?xml version=\"1.0\" encoding=\"gbk\"?>");
            StrBuilder.Append("<DATAPACKET Version=\"2.0\">");
            StrBuilder.Append("<METADATA><FIELDS>");
            int FieldCount = ds.Tables[0].Columns.Count;
            for (int i = 0; i < FieldCount; i++)
            {
                DataColumn dc = ds.Tables[0].Columns[i];
                string FieldType = dc.DataType.Name.ToLower();
                //byte
                if (FieldType.ToLower() == "byte")
                    FieldType = "i2";
                // integer 

                if (FieldType.ToLower() == "int32")
                    FieldType = "i4";
                // int64 
                if (FieldType.ToLower() == "int64")
                    FieldType = "i8";
                // float 
                if ((FieldType.ToLower() == "single"))
                    FieldType = "r8";
                // memo 
                if (FieldType.ToLower() == "string" && dc.MaxLength > 255)
                    FieldType = "bin.hex";
                // bytes 
                if (FieldType.ToLower() == "byte[]")
                    FieldType = "bin.hex";
                if (FieldType.ToLower() == "datetime")
                    FieldType = "dateTime";
                //Result += string.Format("<FIELD attrname=\"{0}\" fieldtype=\"{1}\" ", dc.ColumnName, FieldType);
                if (FieldType.ToLower() == "decimal")
                    StrBuilder.Append(string.Format("<FIELD attrname=\"{0}\" fieldtype=\"fixed\" ", dc.ColumnName));
                else
                    StrBuilder.Append(string.Format("<FIELD attrname=\"{0}\" fieldtype=\"{1}\" ", dc.ColumnName, FieldType));
                if (FieldType == "bin.hex" && dc.DataType.Name.ToLower() == "string")
                    //Result += "SUBTYPE=\"Text\" ";
                    StrBuilder.Append("SUBTYPE=\"Text\" ");
                else if (FieldType.ToLower() == "string")
                {
                    if (dc.MaxLength > 0)
                        //Result += string.Format("WIDTH=\"{0}\" ", dc.MaxLength);
                        StrBuilder.Append(string.Format("WIDTH=\"{0}\" ", dc.MaxLength));
                    else
                        StrBuilder.Append(string.Format("WIDTH=\"{0}\" ", 255));
                }
                else if ((FieldType.ToLower() == "sqldatetime"))
                    StrBuilder.Append("SUBTYPE=\"Formatted\"");
                else if (FieldType == "bin.hex" && dc.DataType.Name.ToLower() != "string" || dc.DataType.Name.ToLower() == "byte[]")
                    StrBuilder.Append("SUBTYPE=\"Binary\"");
                //如果字段类型为decimal,则需要进行特别处理
                //<FIELD attrname="LaborQty" fieldtype="fixed" DECIMALS="8" WIDTH="22"/>
                else if (FieldType.ToLower() == "decimal")
                {
                    //if (dc.MaxLength > 0)
                    //Result += string.Format("WIDTH=\"{0}\" ", dc.MaxLength);
                    StrBuilder.Append(string.Format("DECIMALS=\"8\" WIDTH=\"20\"", dc.MaxLength));
                    //else
                    //StrBuilder.Append(string.Format("DECIMALS=\"{0}\" DECIMALS=\"{1}\"", dc.Ordinal, 20));
                }
                StrBuilder.Append("/>");
            }
            StrBuilder.Append("</FIELDS><PARAMS/></METADATA>");
            StrBuilder.Append("<ROWDATA>");
            int RowCount = ds.Tables[0].Rows.Count;
            for (int i = 0; i < RowCount; i++)
            {
                StrBuilder.Append("<ROW RowState=\"4\" ");
                for (int j = 0; j < FieldCount; j++)
                {
                    DataTable dt = ds.Tables[0];
                    string value = dt.Rows[i].ItemArray[j].ToString();
                    value = value.Replace("\"", "&quot;");
                    StrBuilder.Append(string.Format("{0}=\"{1}\" ",
                        dt.Columns[j].ColumnName,
                        value));
                }
                StrBuilder.Append("/>");
            }
            StrBuilder.Append("</ROWDATA></DATAPACKET>");
            return StrBuilder.ToString();
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值