使用global.asax或Ihttmodule记录Xml

时间:2014-10-22 07:17:44

标签: c# asp.net web-services httprequest httpresponse

在Web服务中,我需要记录每个请求,并且这些请求是xml格式。 问题是每当我试图将数据记录到文本文件中时,我得到一些xml字符的unicode值(我猜是这样)。你能帮我写一些正确的文字吗?截至目前,我的XMl文件如下所示:

<?xml version="1.0" encoding="utf-8" ?>
  <Fields>
    <Field>
      <DealeCode>C001</DealeCode>
      <IMEI>158518121</IMEI>
      <DealerName>Mohan Sales</DealerName>
      <UploadDate>17/10/2014</UploadDate>
    </Field>
  </Fields>

  System.IO.StreamReader reader = new System.IO.StreamReader(HttpContext.Current.Request.InputStream);
  string bodyText = reader.ReadToEnd();
  reader.BaseStream.Seek(0, System.IO.SeekOrigin.Begin);

  System.IO.File.WriteAllText(@"D:\LogFile.txt", bodyText);

以下是编写xml后我的日志文件的样子。

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body>
    <HelloWorld xmlns="http://tempuri.org/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <xmlData>
      <?xml version="1.0" encoding="utf-8" ?>
        <Fields>
          <Field>
            <DealeCode>C001</DealeCode>    
            <IMEI>158518121</IMEI>    
            <DealerName>Mohan Sales</DealerName>    
            <UploadDate>17/10/2014</UploadDate>  
          </Field>  
        </Fields>
      </xmlData>
    </HelloWorld>
  </s:Body>
</s:Envelope>

但是,当我尝试使用XDocument保存我的日志文件时,它会显示非法字符。请在这里帮忙。

1 个答案:

答案 0 :(得分:0)

我的日志文件输出已经被评论者改变了,如果网站在发布文件时已经提出这个问题,他似乎不应该这样做,我故意做了那个未格式化的xml,因为那是我的实际结果。

为了清楚起见,我得到了字符以下 &amp; lt; &amp; gt; &amp; XlgD;等。