UTF编码标签不起作用,以及如何使用c#asp.net在XML文件中添加属性ID

时间:2018-08-20 10:10:16

标签: c# asp.net xml

我试图添加出现在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();
    
        } 
    

    输出结果:

    Output Result

预期的输出结果。忽略注释中的内容。 utf编码是我正在寻找的一种 expected output with utf-encoding tag

0 个答案:

没有答案