XMLDocument,XMLTextWriter和HttpWebResponse类在.NET Core项目中不起作用

时间:2017-02-03 12:43:15

标签: c# .net-core xmldocument xmltextwriter

我正在尝试将控制台应用程序项目集成到a.netcore项目中。在我的控制台应用程序中,项目完全按照我的要求进行操作,并且构建和运行完美,但是当我尝试添加库引用时复制到.net核心Web应用程序时,它建议使用与我不同的库。使用控制台应用程序。当我尝试使用建议的库时,项目将无法构建,或者如果我使用完全相同的引用,它将无法在.net核心Web应用程序中使用。

private HttpWebResponse PutOnUri(string uri, string contentType, string body)
{
    try
    {
        byte[] bodyBytes = Encoding.UTF8.GetBytes(body);

        var client = (HttpWebRequest)HttpWebRequest.Create(uri);
        client.AllowAutoRedirect = false;
        client.AllowWriteStreamBuffering = false;

        client.Method = "PUT";
        client.ContentType = contentType;
        client.ContentLength = bodyBytes.Length;

        client.GetRequestStream().Write(bodyBytes, 0, bodyBytes.Length);
        return (HttpWebResponse)client.GetResponse();
    }
    catch (WebException e)
    {
        return (HttpWebResponse)e.Response;
    }
}


 private XmlDocument LoadXMLFromUri(string uri)
 {
     XmlDocument doc = new XmlDocument();
     doc.Load(uri);
     return doc;
 }

 private string ConvertXmlDocumentToString(XmlDocument doc)
 {
     StringWriter sw = new StringWriter();
     XmlTextWriter tx = new XmlTextWriter(sw);
     doc.WriteTo(tx);
     return sw.ToString();
 }

这些是我在OLD控制台应用中使用的参考,它工作正常:

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;

附加功能(使用XPAth):

private string GetValueFromDocumentByXPath(XmlDocument doc, string xpath)
{
    var nav = doc.CreateNavigator();
    var it = nav.Select(xpath, nameSpaceManager_);
    if (it.MoveNext())
    {
        return it.Current.Value;
    }

        return "";
    }

    private void SetValueToDocumentByXPath(XmlDocument doc, string xpath, string value)
    {
        var nav = doc.CreateNavigator();
        var it = nav.Select(xpath, nameSpaceManager_);
        if (it.MoveNext())
        {
            it.Current.SetValue(value);
        }
    }

1 个答案:

答案 0 :(得分:2)

您需要稍微修改一下代码。

    private XPathDocument LoadXMLFromUri(string uri)
    {
        var req = WebRequest.CreateHttp(uri);
        var resTask = req.GetResponseAsync();
        resTask.Wait();
        XPathDocument doc = new XPathDocument(resTask.Result.GetResponseStream());
        return doc;
    }

    private string ConvertXmlDocumentToString(XmlDocument doc)
    {
        StringWriter sw = new StringWriter();
        XmlWriter tx = XmlWriter.Create(sw);
        doc.WriteTo(tx);
        return sw.ToString();
    }

    private string GetValueFromDocumentByXPath(XPathDocument doc, string xpath)
    {
        var nav = doc.CreateNavigator();
        var it = nav.Select(xpath, nameSpaceManager_);
        if (it.MoveNext())
        {
            return it.Current.Value;
        }

        return "";
    }

    private void SetValueToDocumentByXPath(XPathDocument doc, string xpath, string value)
    {
        var nav = doc.CreateNavigator();
        var it = nav.Select(xpath, nameSpaceManager_);
        if (it.MoveNext())
        {
            it.Current.SetValue(value);
        }
    }

    private HttpWebResponse PutOnUri(string uri, string contentType, string body)
    {
        try
        {
            byte[] bodyBytes = Encoding.UTF8.GetBytes(body);

            var client = (HttpWebRequest)HttpWebRequest.Create(uri);
            client.Method = "PUT";
            client.ContentType = contentType;
            var reqStreamTask = client.GetRequestStreamAsync();
            reqStreamTask.Result.Write(bodyBytes, 0, bodyBytes.Length);
            reqStreamTask.Wait();
            var resTask = client.GetResponseAsync();
            resTask.Wait();
            return (HttpWebResponse) resTask.Result;
        }
        catch (WebException e)
        {
            return (HttpWebResponse)e.Response;
        }
    }