Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate XML Schema in Delphi received by c# web service as a parameter

I have a C# Web Service that returns a XML as a result that will be consumed by a Delphi 7 application. Normally, I would return a .Net XmlDocument class if I had a .Net client, but, for Delphi, I'm returning a string. Below is the C# Web Service Code:

public String ReturnXML()
{
    XmlDocument xmlDoc = GenerateXmlMethod();
    String sXmlResult = String.Empty;
    if (xmlDoc != null)
    {
        using (StringWriter oXml = new StringWriter())
        {
            xmlDoc.Save(oXml);
            sXmlResult = oXml.ToString();
        }
    }
    return sXmlResult;
}

In Delphi, I got the code below from another question here at StachOverflow, and it works perfectly if I had to load the XML and XSD from disk, but I need to load it from memory. Below is my Delphi code now:

procedure TfrmTestador.Button3Click(Sender: TObject);
var
  XML, XSDL, XSDLDom: Variant;
begin
  XSDLDom := CreateOLEObject('MSXML2.DOMDocument.6.0');
  try
    XSDLDom.async := false;
    XSDLDom.load('C:\Temp\XsdFile.xsd');
    XSDL := CreateOLEObject('MSXML2.XMLSchemaCache.6.0');
    try
      XSDL.add('',XSDLDom);
      XML := CreateOLEObject('Msxml2.DOMDocument.6.0');
      try
        XML.validateOnParse := True;
        XML.resolveExternals := True;
        XML.schemas := XSDL;
        XML.load('C:\Temp\XmlFile.xml');
        ShowMessage(XML.parseError.reason);
      finally
        XML := Unassigned;
      end;
    finally
      XSDL := Unassigned;
    end;
  finally
    XSDLDom := Unassigned;
  end;
end;

What would be the Delphi code to load the XSD and the XML from WideString variables, and have it working as the code that loads them from file, validating the XML on a fixed XSD schema that is coded into the application? Is there a better way to return the XML from C# so it is read more easily into Delphi?
Tks for your time!

like image 666
Pascal Avatar asked Sep 13 '10 00:09

Pascal


1 Answers

Your question boils down to code to load the XSD and the XML from WideString variables using "MSXML2.DOMDocument.6.0".

That question is totally independent of Delphi, as you are using the language independent IXMLDOMDocument/DOMDocument from the Microsoft MSXML2 DOM implementation (which has excellent on-line documentation).

The loadXML method will load the XML from a string (which complements the load method, that loads it from a URL).

Your code then will become something like this:

XSDLDom.loadXML(XsdString);
....
XML.loadXML(XmlString);

BTW: Deepak Shenoy has a nice whitepaper on using XML in Delphi that explains more on how to use the DOM in Delphi.

--jeroen

like image 166
Jeroen Wiert Pluimers Avatar answered Sep 25 '22 12:09

Jeroen Wiert Pluimers