Seti Net
Seti Net

Reputation: 743

How to create xmlns:xsi and xsd information in an XML document

I have a Delphi XE application that reads a validated XML file, modifies it and then saves it. The saved version can be validated. I use SML Spy to create the file and to validate it.

Now I need to create a document in memory and save it. The problem is that I cannot figure out how to generate the xmlns and xsd information attributes to the document so that it can be validated.

Upvotes: 4

Views: 5495

Answers (1)

Ken White
Ken White

Reputation: 125757

Actually, despite my comment above I found that the easiest way to do it was not with DeclareNamespace.

Here's an example that doesn't even use a TXMLDocument on the form. Just add xmldom, XMLIntf, and XMLDoc to your implementation uses clause (Xml.xmldom , Xml.XMLIntf, and Xml.XMLDoc for XE2), and then this works:

procedure TForm1.Button1Click(Sender: TObject);
var
  TheDoc: IXmlDocument;
  iNode: IXmlNode;
  xmlText: DOMString;
begin
  TheDoc := NewXMLDocument;
  TheDoc.Version := '1.0';
  TheDoc.Encoding := 'UTF-16';
  iNode := TheDoc.AddChild('test:test_file');
  iNode.SetAttributeNS('xmlns:test', '', 'http://www.foo.com' );
  iNode.SetAttributeNS('xmlns:xsi', '', 'http://www.w3.org/2001/XMLSchema');
  TheDoc.SaveToXML(xmlText);
  Memo1.Lines.Text := xmlText;
end;

The above results in this output in the TMemo:

<?xml version="1.0" encoding="UTF-16"?>
<test:test_file xmlns:test="http://www.foo.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema"/>

Upvotes: 7

Related Questions