FoxPro and .NET Interop
Create and use an XMLReader using wwDotNetBridge?
Gravatar is a globally recognized avatar based on your email address. Create and use an XMLReader using wwDotNetBridge?
  Paul
  All
  Aug 6, 2019 @ 10:38pm

Hi,

I've tried creating an XMLReader from a StringReader, but I can't figure it out.
Here is the latest (of many...) code attempts :

MyComArray = loBridge.CreateArray("System.String")
MyComArray.AddItem(XMLStringToUse)
loValue = loBridge.CreateComValue()
?loValue.SetValueFromCreateInstance("System.IO.StringReader", MyComArray)

*** So far so good, loValue contains the XML string

XMLReader = loBridge.InvokeStaticMethod("System.XML.XmlReader", "Create", lovalue)
*** Hmm, not working.  No error, but XMLReader is null

What should the correct approach be please?

Thanks
Paul

Gravatar is a globally recognized avatar based on your email address. re: Create and use an XMLReader using wwDotNetBridge?
  Rick Strahl
  Paul
  Aug 14, 2019 @ 08:41am

Not sure but I think you need to pass a string to StringReader not an array of string. Also I don't think you need to use ComValue here - you can just create the instance and pass it directy.

Here's the C# code you probably are intending to use:

string xml = "<doc><value>1</value></doc>";

var sr = new StringReader(xml);

// dump output LinqPad
// var s2 = sr.ReadToEnd();
// s2.Dump();  // xml string

var reader = XmlReader.Create(sr);

reader.Dump(); // not null

This works:

do wwDotNetBridge
LOCAL loBridge as wwDotNetBridge
loBridge = GetwwDotnetBridge()

lcXml ="<doc><value>1</value></doc>" 

loReader = loBridge.CreateInstance("System.IO.StringReader",lcXml)
? loReader
loXMLReader = loBridge.InvokeStaticMethod("System.Xml.XmlReader", "Create", loReader)
? loXmlReader

I think you got the namespace wrong in your code - it's not System.XML.XmlReader but System.Xml.XmlReader. Case matters.

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Create and use an XMLReader using wwDotNetBridge?
  Paul
  Rick Strahl
  Aug 17, 2019 @ 07:44pm

Thanks Rick,
Unfortunately that doesn't work though, even though the object is created it isn't something that can be passed to other objects (such as XMLCompiledTransform).
In the end I just wrote a .NET assembly to do the work (probably should have started with that...)
Good tip about using the correct case, I had not noticed my error there.

Paul

Gravatar is a globally recognized avatar based on your email address. re: Create and use an XMLReader using wwDotNetBridge?
  Rick Strahl
  Paul
  Aug 18, 2019 @ 10:43am

Yes - creating a .NET assembly for anything beyond trivial is usually a good idea. I basically add 1 custom .NET assembly to most FoxPro applications these days and just use it to host a variety of interfaces that need to work with .NET code. That way - unless the calls are super simple it's easy to just add a helper in the library and call that from FoxPro.

That said, I think that you could make this work with just wwDotnetBridge. The code below works to create the reader and you can pass just about any object using InvokeMethod() and using ComValue to host any type instances that normally wouldn't work over COM.

But as you found - it's not always easy to figure out the intricacies of conversions needed and using plain .NET code is heaps easier.

+++ Rick ---

© 1996-2024