XElement生成XML的有关问题
XElement生成XML的问题C# codeDataTable dt dsPurchaseInfo.Tables[0]//int rows dt.Rows.Countint
XElement生成XML的问题
C# code DataTable dt = dsPurchaseInfo.Tables[0]; //int rows = dt.Rows.Count; int rows = 1; XElement rtnXml = new XElement("PURCHASE"); for (int i = 0; i < rows; i++) { DataRow row = dt.Rows[i]; XElement tmpjl = new XElement("PURCHASE_INFO", new XElement("HOSPITAL_CODE", row["HOSPITAL_CODE"].ToString().Trim()), new XElement("PRODUCT_ID", row["PRODUCT_ID"].ToString().Trim()), new XElement("REQUEST_QTY", row["REQUEST_QTY"].ToString().Trim()), new XElement("SALE_ORG_CODE", row["SALE_ORG_CODE"].ToString().Trim()) ); rtnXml.Add(tmpjl); } string PurchaseXml = rtnXml.ToString(); textBox2.Text = PurchaseXml;
textbox2.text显示的内容为:
<PURCHASE>
<PURCHASE_INFO>
<HOSPITAL_CODE>370000737997</HOSPITAL_CODE>
<PRODUCT_ID>9370100000000</PRODUCT_ID>
<REQUEST_QTY>10000.00</REQUEST_QTY>
<SALE_ORG_CODE>37464</SALE_ORG_CODE>
</PURCHASE_INFO>
</PURCHASE>
但是问题出来了.
对方webservice接收到解密后,服务出错.
经过跟踪,找到问题,
跟踪为:
textbox2.text
"<PURCHASE>\r\n <PURCHASE_INFO>\r\n <HOSPITAL_CODE>370000737997</HOSPITAL_CODE>\r\n <PRODUCT_ID>9370100000000</PRODUCT_ID>\r\n <REQUEST_QTY>10000.00</REQUEST_QTY>\r\n <SALE_ORG_CODE>37464</SALE_ORG_CODE>\r\n </PURCHASE_INFO>\r\n</PURCHASE>"
中间发现有\r\n 字符,这个原因可能导致的问题,因为我用静态值形成以上格式,对方服务解析是可以的.正确
如何避免呢?
[解决办法]C# codeStringBuilder sb = new StringBuilder();StringWriter sw = new StringWriter(sb);rtnXml.Save(sw, SaveOptions.DisableFormatting);string xml = sb.ToString();
[解决办法]
用字符串拼接或者格式化字符串。
[解决办法]
[解决办法]
\\r\\n