XMLDocument、XMLTextWriter 和 HttpWebResponse Class 在 .NET Core 项目中不起作用

XMLDocument, XMLTextWriter and HttpWebResponse Class will not work in .NET Core project

我正在尝试将控制台应用程序项目集成到 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();
 }

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

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);
        }
    }

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

    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;
        }
    }