Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download and open XML file via SFTP?

I need to open XML file (create XmlDocument) without creating local copy. Using SSH.NET, I came up with this code:

var connectionInfo = new ConnectionInfo("host",
    "username",
    new PasswordAuthenticationMethod("username", "password"));

using (var client = new SftpClient(connectionInfo))
{
    client.Connect();

    System.IO.MemoryStream mem = new System.IO.MemoryStream();

    client.DownloadFile("filename.xml", mem);

    mem.Position=0;

    using(XmlReader reader = XmlReader.Create(mem))
    {
        var docc = new XmlDocument();
        docc.Load(mem);
    }

    client.Disconnect();
}

But is gets stuck on docc.Load(mem). What could be the problem?

mem object looks like this:

enter image description here

like image 680
nicks Avatar asked Jan 31 '26 21:01

nicks


1 Answers

Note that here:

using(XmlReader reader = XmlReader.Create(mem))
{
    var docc = new XmlDocument();
    docc.Load(mem);
}

You are not using variable reader at all. Either change to

using(XmlReader reader = XmlReader.Create(mem))
{
    var docc = new XmlDocument();
    docc.Load(reader);
}

or remove reader at all:

docc.Load(mem);
like image 180
Evk Avatar answered Feb 02 '26 12:02

Evk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!