我试图添加出现在xml文件中的utf编码标签。但我尝试的代码似乎无效。我也是该领域的新手。 因此,如果有人能告诉我在下面的代码中何处添加适当的代码,我将不胜感激。我的需求是:
如何添加“ salemastid”作为ID属性以及订单 即;
<Orders ID=24> where 24 is the salemastid
感谢您的帮助。您还可以提供适当的参考链接。 在此先感谢您的时间。
static XmlDocument xdoc = new XmlDocument();
protected void xmlFile(DataSet ds) //ds contains sales details; i.e list of products along with quantity and unit
{
string salemastid = lbl_salemastid.Text; //label control (asp.net)
int i = 0, j=0;
String str = "salemastid:" + salemastid;
DataTable dt = ds.Tables[0];
string xml = "<Orders>" ;
while (j < dt.Rows.Count)
{
int slno = j + 1;
string sl = slno.ToString();
xml += "<SlNo>" + sl +"</SlNo>" +
"<PdtName>" + dt.Rows[j][0].ToString() + "</PdtName>" +
"<Unit>" + dt.Rows[j][1].ToString() + "</Unit>" +
"<Qty>" + dt.Rows[j][2].ToString() + "</Qty>";
j++;
}
xml += "</Orders>";
xdoc.LoadXml(xml);
//Here the xml is prepared and loaded in xml DOM.
xdoc.Save(Server.MapPath("Newsales.xml"));
//tried to add the commented codes inorder to add the encoding tags.
//XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
//doc.AppendChild(docNode);
System.IO.MemoryStream stream = new System.IO.MemoryStream();
XmlTextWriter xwriter = new XmlTextWriter(stream, System.Text.Encoding.UTF8);
xdoc.WriteTo(xwriter);
xwriter.Flush();
Response.Clear();
Encoding.UTF8.GetString(stream.ToArray());
byte[] byteArray = stream.ToArray();
Response.AppendHeader("Content-Disposition", "filename=OrderRequest.xml");
Response.AppendHeader("Content-Length", byteArray.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(byteArray);
xwriter.Close();
stream.Close();
}
输出结果: