Search code examples
c#.net-corexmldocumentxmltextwriter

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


I am trying to integrate A console app project into to a.netcore project. In my console app the project does exactly what I want it to do and build and run perfectly well but when copied into the .net core web application when I try to add the library references it is suggesting a completely different library from the ones I am using the console app. when I try to use the suggested libraries the project will not build or if I am using the exact same reference it will not work in the .net core web app.

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

These are the reference I am using in the OLD Console app and it works fine:

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

Additional functions (Uses 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);
        }
    }

Solution

  • You will need to modify your code a bit.

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