-2

我有这段代码,它可以从调用 Web API 应用程序的 Winforms 应用程序正常工作(它成功地将 XML 文件复制到服务器):

public static string SendXMLFile(string xmlFilepath, string uri, int timeout)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);

    request.KeepAlive = false;
    request.ProtocolVersion = HttpVersion.Version10;
    request.ContentType = "application/xml";
    request.Method = "POST";

    StringBuilder sb = new StringBuilder();
    using (StreamReader sr = new StreamReader(xmlFilepath))
    {
        String line;
        while ((line = sr.ReadLine()) != null)
        {
            sb.AppendLine(line);
        }
        MessageBox.Show(sb.ToString());
        byte[] postBytes = Encoding.UTF8.GetBytes(sb.ToString());

        if (timeout < 0)
        {
            request.ReadWriteTimeout = timeout;
            request.Timeout = timeout;
        }

        request.ContentLength = postBytes.Length;

        try
        {
            Stream requestStream = request.GetRequestStream();

            requestStream.Write(postBytes, 0, postBytes.Length);
            requestStream.Close();

            HttpWebResponse response = null; //<= uncomment for older versions of .NET
            try
            {
                response = (HttpWebResponse)request.GetResponse(); //<= uncomment for older versions of .NET (comment out using)
            }
            finally
            {
                IDisposable disposableResponse = response as IDisposable;
                if (disposableResponse != null) disposableResponse.Dispose();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            request.Abort();
            return string.Empty;
        }
    }
}

但是,当试图找出与此工作代码尽可能相似的代码在尝试将 xml 文件从手持设备发送到同一服务器时不起作用的原因时,我尝试使用 XmlTextReader 代替(如此处所建议( “你在这里没有使用 XML 类和方法”),我在这里详细介绍了我的辛苦);唯一的区别是:

using (StreamReader sr = new StreamReader(xmlFilepath))
{
    String line;
    while ((line = sr.ReadLine()) != null)
    {
        sb.AppendLine(line);
    }
    MessageBox.Show(sb.ToString());
    byte[] postBytes = Encoding.UTF8.GetBytes(sb.ToString());

...在工作(StreamReader)代码中更改为:

XmlTextReader reader = new XmlTextReader(xmlFilepath);
while (reader.Read())
{
    switch (reader.NodeType)
    {
        case XmlNodeType.Element: // The node is an Element.
            sb.Append("<" + reader.Name);

            while (reader.MoveToNextAttribute()) // Read attributes.
                sb.Append(" " + reader.Name + "='" + reader.Value + "'");
            sb.Append(">");
            sb.Append(">");
            break;
        case XmlNodeType.Text: //Display the text in each element.
            sb.Append(reader.Value);
            break;
        case XmlNodeType.EndElement: //Display end of element.
            sb.Append("</" + reader.Name);
            sb.Append(">");
            break;
    }
}

MessageBox.Show(sb.ToString());
byte[] postBytes = Encoding.UTF8.GetBytes(sb.ToString());

使用 StreamReader 代码,读取的数据看起来就像在磁盘上一样:

在此处输入图像描述

但是,使用 XmlTextReader 时,这一切都像从大炮中射出的什锦饭一样混杂在一群河马身上:

在此处输入图像描述

...并且 XmlTextReader 版本失败,并显示“远程服务器返回错误:(500) 内部服务器错误。

所以 StreamReader.ReadLine 比 XmlTextReader.Read 更适用于 XML 文件;看起来很奇怪......我在这里做错了吗?

4

1 回答 1

2

默认情况下,XmlTextReader将忽略空格。您需要专门告诉它包含空格,并开始在您的开关中处理空格。

另请注意,所有XmlReader实现都是一次性的,因此请将它们包装在using.

XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = false;
using(XmlReader reader = XmlReader.Create(xmlFilepath, settings)) {
    while (reader.Read())
    {
        switch (reader.NodeType)
        {
            case XmlNodeType.Element: // The node is an Element.
                sb.Append("<" + reader.Name);

                while (reader.MoveToNextAttribute()) // Read attributes.
                    sb.Append(" " + reader.Name + "='" + reader.Value + "'");
                sb.Append(">");
                sb.Append(">");
                break;
            case XmlNodeType.Text: 
            case XmlNodeType.Whitespace:
                sb.Append(reader.Value);
                break;
            case XmlNodeType.EndElement: //Display end of element.
                sb.Append("</" + reader.Name);
                sb.Append(">");
                break;
        }
    }
}
MessageBox.Show(sb.ToString());
byte[] postBytes = Encoding.UTF8.GetBytes(sb.ToString());
于 2014-08-29T02:27:25.497 回答