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 checked. I use SML Spy to create a file and 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 for the document so that it can be checked.

+4
source share
1 answer

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

Here is an example that doesn't even use TXMLDocument in the form. Just add xmldom , XMLIntf and XMLDoc to your sentence using the sentence ( Xml.xmldom , Xml.XMLIntf and Xml.XMLDoc for XE2) and then it 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 are output to 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"/> 
+6
source

All Articles